How to run Nginx within a Docker container without halting?

143

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

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.

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.

This question is tagged with linux nginx docker

~ Asked on 2013-09-17 23:03:14

The Best Answer is


180

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

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

~ Answered on 2013-09-17 23:04:48


200

To expand on Charles Duffy's answer, Nginx uses the daemon off directive 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.

To run in foreground:

nginx -g 'daemon off;'

To run in background:

nginx

~ Answered on 2015-01-22 22:38:23


Most Viewed Questions: