How to start Nginx in a Docker container without stopping?

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

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

The problem is that the Nginx method works, the initial process immediately spawns the Nginx master process and some workers, and then exits. Because Docker only looks at the PID of the original command, the container stops.

How to prevent container from stopping? I need to say that it binds to the first child process or stops the original Nginx process.

+82
linux docker nginx
Sep 17 '13 at 23:03
source share
7 answers

nginx , like all well-managed programs, may not be configured for self-demining.

Use the daemon off configuration directive described at http://wiki.nginx.org/CoreModule .

+120
Sep 17 '13 at 23:04 on
source share

To extend Charles Duffy's answer, Nginx uses the daemon off directive to run in the foreground. If this is inconvenient to place in the configuration file, we can specify this directly on the command line. This allows you to easily work in debug mode (in the foreground) and directly switch to work in production mode (in the background) by changing the command line arguments.

To run in the foreground:

 nginx -g 'daemon off;' 

To run in the background:

 nginx 
+115
Jan 22 '15 at 22:38
source share

To expand John's answer, you can also use the Dockerfile CMD command as follows (in case you want it to run without additional arguments)

 CMD ["nginx", "-g", "daemon off;"] 
+36
Nov 27 '16 at 10:52
source share

Adding this command to the Dockerfile may disable it:

 RUN echo "daemon off;" >> /etc/nginx/nginx.conf 
+6
Apr 26 '17 at 10:19 on
source share

Here you have an example of a Dockerfile that runs nginx. As Charles mentioned, he uses daemon off configuration:

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

+5
Sep 17 '13 at 23:38
source share

It is also useful to use supervisord or runit [1] to manage services.

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

+3
Aug 25 '14 at 2:52
source share

To add the answers of Tomer and Charles,

The syntax for running nginx in forground in a Docker container using Entrypoint:

 ENTRYPOINT nginx -g 'daemon off;' 

Not directly related, but to run multiple commands with Entrypoint:

 ENTRYPOINT /bin/bash -x /myscripts/myscript.sh && nginx -g 'daemon off;' 
0
Dec 24 '18 at 10:02
source share



All Articles