Home >Operation and Maintenance >Nginx >What is the reason why docker uses daemon off when running nginx?
Question
1. Why does the docker container hang when it is running?
By default, the docker container will use the first process inside the container, that is, the program with pid=1, as the basis for whether the docker container is running. If the docker container pid hangs, the docker container will exit directly.
2. When docker runs, use command as the internal command of the container. If you use nginx, then the nginx program will run in the background. At this time, nginx is not the program with pid 1, but the bash executed. This bash After executing the nginx command, it hangs, so the container also exits. The same reason as yours, after pm2 start, the pid of bash is 1, then bash will exit after execution, so the container also exits.
Below I use examples to tell why we do this! !
touch file get_pid
echo "pid of this script: $$" echo "ppid of this script: $ppid" echo "uid of this script: $uid" #nginx -g 'daemon off;'
At this time we start the container to execute this sh file
odtoy:~ zhaojunlike$ eval `docker-machine env default` godtoy:~ zhaojunlike$ cd workspace/ godtoy:workspace zhaojunlike$ ls docker nodejs php pid_get godtoy:workspace zhaojunlike$ vim pid_get godtoy:workspace zhaojunlike$ docker run -v `pwd`/pid_get:/pid_get:ro --rm --workdir=/ nginx bash /pid_get pid of this script: 1 ppid of this script: 0 uid of this script: 0 godtoy:workspace zhaojunlike$
After the container executes pid_get, the container It automatically exits. At this time, the pid of the current bash running is printed out as 1.
So, if we want the container not to hang, then non-daemon execution is a must. Of course, we can also execute it inside a container.
godtoy:workspace zhaojunlike$ docker run -it nginx bash root@a8baa5fe77f0:/# nginx root@a8baa5fe77f0:/# godtoy:workspace zhaojunlike$
We use the -it parameter Can connect to the pipe inside the container and then we use nginx command inside the container. Finallyctrl p q
After exiting the container, the container is still running.
The above is the detailed content of What is the reason why docker uses daemon off when running nginx?. For more information, please follow other related articles on the PHP Chinese website!