Linux 如何在不停止的情况下在 Docker 容器中运行 Nginx?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18861300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:52:43  来源:igfitidea点击:

How to run Nginx within a Docker container without halting?

linuxnginxdocker

提问by Seldo

I have Nginx installed on a Docker container, and am trying to run it like this:

我在 Docker 容器上安装了 Nginx,并尝试像这样运行它:

docker run -i -t -p 80:80 mydockerimage /usr/sbin/nginx

The problem is that the way Nginx works, is that the initial process immediately spawns a master Nginx process and some workers, and then quits. Since Docker is only watching the PID of the original command, the container then halts.

问题在于 Nginx 的工作方式是,初始进程立即生成一个主 Nginx 进程和一些工作进程,然后退出。由于 Docker 只监视原始命令的 PID,因此容器会停止。

How do I prevent the container from halting? I need to be able to tell it to bind to the first child process, or stop Nginx's initial process from exiting.

如何防止容器停止?我需要能够告诉它绑定到第一个子进程,或者阻止 Nginx 的初始进程退出。

采纳答案by Charles Duffy

nginx, like all well-behaved programs, can be configured not to self-daemonize.

nginx,像所有表现良好的程序一样,可以配置为不自我守护。

Use the daemon offconfiguration directive described in http://wiki.nginx.org/CoreModule.

使用http://wiki.nginx.org/CoreModule 中daemon off描述的配置指令。

回答by creack

Here you have an example of a Dockerfile that runs nginx. As mentionned by Charles, it uses the daemon offconfiguration:

这里有一个运行 nginx 的 Dockerfile 示例。正如查尔斯提到的,它使用daemon off配置:

https://github.com/darron/docker-nginx-php5/blob/master/Dockerfile#L17

https://github.com/darron/docker-nginx-php5/blob/master/Dockerfile#L17

回答by Kunthar

It is also good idea to use supervisord or runit[1] for service management.

使用 supervisord 或 runit[1] 进行服务管理也是一个好主意。

[1] https://github.com/phusion/baseimage-docker

[1] https://github.com/phusion/baseimage-docker

回答by johntellsall

To expand on Charles Duffy's answer, Nginx uses the daemon offdirective to run in the foreground. If it's inconvenient to put this in the configuration file, we can specify it directly on the command line. This makes it easy to run in debug mode (foreground) and directly switch to running in production mode (background) by changing command line args.

为了扩展 Charles Duffy 的回答,Nginx 使用daemon off指令在前台运行。如果不方便把这个放在配置文件中,我们可以直接在命令行指定。这使得在调试模式(前台)下运行很容易,并通过更改命令行参数直接切换到在生产模式(后台)下运行。

To run in foreground:

在前台运行:

nginx -g 'daemon off;'

To run in background:

在后台运行:

nginx

回答by Tomer Ben David

To expand on John's answer you can also use the DockerfileCMDcommand as following (in case you want it to self start without additional args)

要扩展约翰的答案,您还可以使用以下DockerfileCMD命令(如果您希望它在没有额外参数的情况下自行启动)

CMD ["nginx", "-g", "daemon off;"]

回答by Afshin Mehrabani

Adding this command to Dockerfile can disable it:

将此命令添加到 Dockerfile 可以禁用它:

RUN echo "daemon off;" >> /etc/nginx/nginx.conf

回答by Nitb

To add Tomer and Charles answers,

要添加 Tomer 和 Charles 的答案,

Syntax to run nginx in forground in Docker container using Entrypoint:

使用 Entrypoint 在 Docker 容器中的前台运行 nginx 的语法:

ENTRYPOINT nginx -g 'daemon off;' 

Not directly related but to run multiple commands with Entrypoint:

不直接相关,但使用 Entrypoint 运行多个命令:

ENTRYPOINT /bin/bash -x /myscripts/myscript.sh && nginx -g 'daemon off;' 

回答by Alejandro Teixeira Mu?oz

For all who come here trying to run a nginx image in a docker container, that will run as a service

对于所有来这里尝试在 docker 容器中运行 nginx 映像的人来说,这将作为服务运行

As there is no whole Dockerfile, here is my whole Dockerfilesolving the issue.

由于没有完整的 Dockerfile,这是我Dockerfile解决问题的全部方法。

Nice and working. Thanks to all answers here in order to solve the final nginx issue.

很好,工作。感谢这里的所有答案,以解决最终的 nginx 问题。

FROM ubuntu:18.04
MAINTAINER stackoverfloguy "[email protected]"
RUN apt-get update -y
RUN apt-get install net-tools nginx ufw sudo -y
RUN adduser --disabled-password --gecos '' docker
RUN adduser docker sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER docker
RUN sudo ufw default allow incoming
RUN sudo rm /etc/nginx/nginx.conf
RUN sudo rm /etc/nginx/sites-available/default
RUN sudo rm /var/www/html/index.nginx-debian.html
VOLUME /var/log
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx
VOLUME /var/run
COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY content/* /var/www/html/
COPY Dockerfile /var/www/html
COPY start.sh /etc/nginx/start.sh
RUN sudo chmod +x /etc/nginx/start.sh
RUN sudo chmod -R 777 /var/www/html
EXPOSE 80
EXPOSE 443
ENTRYPOINT sudo nginx -c /etc/nginx/nginx.conf -g 'daemon off;'

And run it with:

并运行它:

docker run -p 80:80 -p 443:443 -dit