I have made a docker container from nginx running in background this way:

docker run -d --name nginx1 -p 49699:80 nginx

When I go to localhost:49699 on a browser with cleared cache it works properly as I get the nginx vanilla index.html page

But then, if I launch another container in interactive mode first, and then exit and launch it again it doesn't works. So what I mean here, is that if I first stop and remove container nginx1 mentioned above, and then I:

docker run -it --name nginx2 -p 49699:80 nginx bash

I create a container called "nginx2" and I immediately get into it's bash. The I hit exit without changing anything inside the container. And since it was interactive, the container stops after I exited the bash. So I the re-start it with docker start nginx2 and it's running with the expect port forwarding as marked by docker ps.

If I then go to localhost:4699 it doesn't shows me nginx.

So it seems that if a nginx container was created in interactive mode. Even if we get out and start the process again with docker start <nginx_container> then nginx won't actually work. As if the interactive mode the container was created with is a process that runs above the nginx processes running inside the container.

I am just trying to understand what the "interactive" mode does to a container that is supposed to have had another process (like nginx) running and ready for port-forwarding. Does interactive mode kills nginx processes?


1 Answer 1


Basically, containers can only run a single process. When you run:

docker run -d --name nginx1 -p 49699:80 nginx

The container will start by running its CMD. If we look at the Dockerfile for the official Nginx container we can see that the CMD is:

CMD ["nginx", "-g", "daemon off;"]

So when you start the container it's going to run that command which will start Nginx. You also have the option of passing in your own command and overriding the CMD that is in the Dockerfile. We can see options for that in the Docker documentation for run.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

After the IMAGE name we can specify an optional COMMAND and a list of arguments. When you ran:

docker run -it --name nginx2 -p 49699:80 nginx bash

You told docker not to run the containers default command but instead run bash. Which is why Nginx isn't running.

You could omit the bash and daemon flag like this:

docker run --name nginx1 -p 49699:80 nginx

Which would run the container in the foreground but you wouldn't be able to run any commands on the container, all you would be able to do is see the Nginx logs in your shell.

Instead I think you are looking for this:

docker run -d --name nginx_test -p 49699:80 nginx

This would start a container called nginx_test in the background using the -d daemon flag. I also often use --rm which tells Docker to delete the container when it has stopped. After the container has started you can connect to it and run commands using this:

docker exec -it nginx_test bash

Which will let you connect to your running container and run additional commands.

  • 1
    Thanks, this is exactly what I was looking for. Sounds like the reason since I've tried running docker exec -d nginx_test nginx -g "daemon off;" and then that container which I though was not going to run nginx never because initially I started it interactively with bash, it's now executing nginx and it's reachable from the port. So yeah, it seams that when I docker run with bash it overwrote the CMD to start nginx, and that's why even though I started the container again nginx was not working because I killed that original CMD that would turn it on Apr 13, 2023 at 0:17
  • 1
    I would add an upvote but I need more reputation to do so Apr 13, 2023 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.