How to run Nginx within a Docker container without halting?
Solution 1
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.
Solution 2
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
Solution 3
To expand on John's answer you can also use the Dockerfile
CMD
command as following (in case you want it to self start without additional args)
CMD ["nginx", "-g", "daemon off;"]
Solution 4
Just FYI, as of today (22 October 2019) official Nginx docker images all have line:
CMD ["nginx", "-g", "daemon off;"]
Solution 5
Adding this command to Dockerfile can disable it:
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
Related videos on Youtube
Seldo
Updated on July 08, 2022Comments
-
Seldo almost 2 years
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.
-
Seldo over 10 yearsThanks! To clarify, that means editing /etc/nginx/nginx.conf and adding "daemon off;" at the top (i.e. not inside a server or other directive)
-
Leonid Shevtsov over 10 yearsI'm somewhat concerned that
daemon off
isn't endorsed by nginx. -
Charles Duffy over 10 years@LeonidShevtsov ...you mean wasn't endorsed for production use prior to 1.0.9. The standing caveat, about in-place upgrades, doesn't matter to folks doing things The Docker Way.
-
Ciro Santilli OurBigBook.com almost 10 yearsAnd the offical Nginx Dockerfile at: github.com/dockerfile/nginx/blob/master/Dockerfile#L15
-
Charles Duffy about 7 yearsThey seem to have a redirection in place server-side now (to nginx.org/en/docs/ngx_core_module.html).
-
red888 over 6 yearsCan someone explain what "-g" actually is? I can't find this switch in the docs only this one example with nginx using it.
-
Charles Duffy over 6 years@red888, sets a global configuration option.
-
prayagupa over 4 years
-
forresthopkinsa over 3 yearsNitb is adding to Tomer, Tomer is adding to John, and John is adding to Charles. It would be nice if this site had a public editing function.
-
Max Allan almost 3 yearsNot very useful without your "start.sh" script. And if you have 777 permissions on your HTML content, if your container is compromised, attackers can change the content. Why does any user need to write to anything in your container? And why do you add the docker user and then use sudo to do everything? Again if you are compromised : having sudo could be a problem, you don't need it. Keep your container slim. Or use the nginx image from docker hub written by people who understand security.
-
blissweb over 2 yearsAnd without the semicolon at the end it dies
-
Tom Ellis over 2 yearsStrangely
-g 'daemon off;'
doesn't seem to work for me. Only putting that line in the config file seems to work. -
zahra_oveyedzade about 2 yearsThis line of code saved me :
ENTRYPOINT sudo nginx -c /etc/nginx/nginx.conf -g 'daemon off;'
. Thank you. -
Firsh - justifiedgrid.com almost 2 years@Seldo nginx: [emerg] "daemon" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1
-
Charles Duffy almost 2 years@Firsh-justifiedgrid.com, as the docs say, it's only allowed in the
main
context. I'd need to see how/where yourconf.d/nginx.conf
is included into the larger configuration to know which context is active for the line in question; no "edit this specific file" rule can be always accurate for every possible system, without knowing how the rest of that system is configured. -
Firsh - justifiedgrid.com almost 2 years@CharlesDuffy Thank you I found the larger configuration!