Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason why docker runs nginx to use daemon off?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the reason why docker should use daemon off to run nginx. It is very detailed and has certain reference value. Friends who are interested must finish it!

If you encounter this problem, we should talk about the process principle of the docker container. Basically, people who have known about docker are aware of several isolation methods of docker, so the process is also isolated.

problem

Why did the 1.docker container hang up when it was running?

By default, the docker container takes the first process within the container, that is, the program of pid=1, as the basis for whether the docker container is running or not. If the docker container pid fails, then the docker container will exit directly.

2.docker run uses command as the internal command of the container. If you use nginx, the nginx program will run in the background. At this time, nginx is not a program with a pid of 1, but an executing bash. The bash executes the nginx instruction and then hangs, so the container exits. For the same reason as yours, after pm2 start, the pid of bash is 1, then the bash will exit after execution, so the container will exit.

Now I'll use an example to tell why we do Thiskeeper!

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 point, we start the container to execute the sh file

Odtoy:~ zhaojunlike$ eval `docker-machine env default`godkeeper: ~ zhaojunlike$ cd WorkSpace/godtoy:WorkSpace zhaojunlike$ lsdocker nodejs php pid_getgodtoy:WorkSpace zhaojunlike$ vim pid_getgodtoy:WorkSpace zhaojunlike$ docker run-v `pwd` / pid_get:/pid_get:ro-- rm-- workdir=/ nginx bash / pid_getPID of this script: 1PPID of this script: 0UID of this script: 0godtoy:WorkSpace zhaojunlike$

After the container executes the pid_get, the container automatically exits, and at this point, it prints that the current bash is running with a pid of 1.

So, if we want to keep the container alive, then unguarded execution is necessary, and of course the following can also be done inside a container.

Godtoy:WorkSpace zhaojunlike$ docker run-it nginx bashroot@a8baa5fe77f0:/# nginxroot@a8baa5fe77f0:/# godtoy:WorkSpace zhaojunlike$

We use the-it parameter to connect to the pipe inside the container, and then we use the nginx command inside the container. Finally, after Ctrl+P+Q exits the container, the container is still running.

Summary

If the landlord needs to use node in docker, then there is no need to install tools such as pm2. Directly node, if you are afraid that your container will die, you can add restart and other related parameters such as `docker run. -- restart=always

The above is all the contents of the article "Why docker runs nginx to use daemon off". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report