debugdockerMinor
Why does running a nginx image in docker interactive mode makes nginx not working
Viewed 0 times
whyimageinteractivedockermodemakesworkingnginxrunningdoes
Problem
I have made a docker container from nginx running in background this way:
When I go to
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:
I create a container called "nginx2" and I immediately get into it's bash. The I hit
If I then go to
So it seems that if a nginx container was created in interactive mode. Even if we get out and start the process again with
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?
docker run -d --name nginx1 -p 49699:80 nginxWhen I go to
localhost:49699 on a browser with cleared cache it works properly as I get the nginx vanilla index.html pageBut 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 bashI 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 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?
Solution
Basically, containers can only run a single process. When you run:
The container will start by running its
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
After the
You told docker not to run the containers default command but instead run
You could omit the bash and daemon flag like this:
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:
This would start a container called
Which will let you connect to your running container and run additional commands.
docker run -d --name nginx1 -p 49699:80 nginxThe 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 bashYou 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 nginxWhich 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 nginxThis 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 bashWhich will let you connect to your running container and run additional commands.
Code Snippets
docker run -d --name nginx1 -p 49699:80 nginxdocker run [OPTIONS] IMAGE [COMMAND] [ARG...]docker run -it --name nginx2 -p 49699:80 nginx bashdocker run --name nginx1 -p 49699:80 nginxdocker run -d --name nginx_test -p 49699:80 nginxContext
StackExchange DevOps Q#17665, answer score: 2
Revisions (0)
No revisions yet.