How to run Nginx within a Docker container without halting?

187,882

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;"]

e.g. https://github.com/nginxinc/docker-nginx/blob/23a990403d6dbe102bf2c72ab2f6a239e940e3c3/mainline/alpine/Dockerfile#L117

Solution 5

Adding this command to Dockerfile can disable it:

RUN echo "daemon off;" >> /etc/nginx/nginx.conf
Share:
187,882

Related videos on Youtube

Seldo
Author by

Seldo

Updated on July 08, 2022

Comments

  • Seldo
    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
    Seldo over 10 years
    Thanks! 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
    Leonid Shevtsov over 10 years
    I'm somewhat concerned that daemon off isn't endorsed by nginx.
  • Charles Duffy
    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
    Ciro Santilli OurBigBook.com almost 10 years
    And the offical Nginx Dockerfile at: github.com/dockerfile/nginx/blob/master/Dockerfile#L15
  • Charles Duffy
    Charles Duffy about 7 years
    They seem to have a redirection in place server-side now (to nginx.org/en/docs/ngx_core_module.html).
  • red888
    red888 over 6 years
    Can 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
    Charles Duffy over 6 years
    @red888, sets a global configuration option.
  • prayagupa
    prayagupa over 4 years
    CMD will be CMD ["nginx", "-g", "daemon off;"] for docker
  • forresthopkinsa
    forresthopkinsa over 3 years
    Nitb 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
    Max Allan almost 3 years
    Not 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
    blissweb over 2 years
    And without the semicolon at the end it dies
  • Tom Ellis
    Tom Ellis over 2 years
    Strangely -g 'daemon off;' doesn't seem to work for me. Only putting that line in the config file seems to work.
  • zahra_oveyedzade
    zahra_oveyedzade about 2 years
    This line of code saved me : ENTRYPOINT sudo nginx -c /etc/nginx/nginx.conf -g 'daemon off;'. Thank you.
  • Firsh - justifiedgrid.com
    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
    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 your conf.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
    Firsh - justifiedgrid.com almost 2 years
    @CharlesDuffy Thank you I found the larger configuration!