Dockerized nginx is not starting Dockerized nginx is not starting nginx nginx

Dockerized nginx is not starting


As of now, the official nginx image uses this to run nginx (see the Dockerfile):

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

In my case, this was enough to get it to start properly. There are tutorials online suggesting more awkward ways of accomplishing this but the above seems quite clean.


Docker container runs as long as the command you specify with CMD, ENTRTYPOINT or through the command line is running. In your case the service command finishes right away and the whole container is shut down.

One way to fix this is to start nginx directly from the command line (make sure you don't run it as a daemon).

Another option is to create a small script which starts the service and then sleeps forever. Something like:

#!/bin/bashservice nginx startwhile true; do sleep 1d; done

and run this instead of directly running the service command.

A third option would be to use something like runit or similar program, instead of the normal service.


Using docker-compose:

To follow the recommended solution, add to docker-compose.yml:

command: nginx -g "daemon off"

I also found I could simply add to nginx.conf:

daemon off;

...and continue to use in docker-compose.yml:

command: service nginx start

...although it would make the config file less portable outside docker.