nginx -g daemon off

来源:4-1 前后端项目Dockerfile&项目实战配置

Han

2019-11-03 19:24:19

daemon off是什么意思?网上找了些回答还是不太理解

写回答

2回答

Brian

2019-11-03

这里有一个很好的解释:

在nginx Docker的官方网页上有一句话:

If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for nginx to stay in the foreground, so that Docker can track the process properly (otherwise your container will stop immediately after starting)!

加上了daemon off,nginx才能一直在后台持续运行,否则就会被docker进程终止,因为docker默认会终止pid为1的进程。

Docker 容器启动时,默认会把容器内部第一个进程,也就是pid=1的程序,作为docker容器是否正在运行的依据,如果 docker 容器pid=1的进程挂了,那么docker容器便会直接退出。

Docker未执行自定义的CMD之前,nginx的pid是1,执行到CMD之后,nginx就在后台运行,bash或sh脚本的pid变成了1。

所以一旦执行完自定义CMD,nginx容器也就退出了。

0

Han

提问者

2019-11-03

而且还有个疑问是nginx -g是配置信息用的,配置后不用再执行nginx之类的去启用nginx服务吗

0

0 学习 · 1842 问题

查看课程