再返回Nginx的docker倉庫仔細查看說明,其實是有說明的 /捂臉~
就是在CMD后面加上exec nginx -g 'daemon off;'
,如下:
command: /bin/bash -c "echo 'hello' && exec nginx -g 'daemon off;'"
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)! Running nginx in debug mode Images since version 1.9.8 come with nginx-debug binary that produces verbose output when using higher log levels. It can be used with simple CMD substitution: $ docker run --name my-nginx -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx nginx-debug -g 'daemon off;' Similar configuration in docker-compose.yml may look like this: web: image: nginx volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro command: [nginx-debug, '-g', 'daemon off;']
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)!
-- 這里的CMD
適用 docker-compose.yml 中的entrypoint
和command
,以及 Dockerfile 中的ENTRYPOINT
和CMD
。
也就是說:
Docker 容器啟動時,默認會把容器內部第一個進程,也就是
pid=1
的程序,作為docker容器是否正在運行的依據,如果 docker 容器pid=1的進程掛了,那么docker容器便會直接退出。Docker未執行自定義的CMD之前,nginx的pid是1,執行到CMD之后,nginx就在后台運行,bash或sh腳本的pid變成了1。
所以一旦執行完自定義CMD,nginx容器也就退出了。
https://hub.docker.com/_/nginx/