Docker-nginx: Nginx is not starting with Docker-compose

Created on 21 Aug 2019  路  5Comments  路  Source: nginxinc/docker-nginx

Hey guys,

Been pulling my hair out for the past several hours trying to to get a working container running nginx. I am able to deploy it with:

nginx:
container_name: nginx
hostname: reverse
image: nginx
ports:
- 80:80
- 443:443
volumes:
- ${D_DRIVE}/nginx/conf:/etc/nginx/
- ${D_DRIVE}/nginx/cert:/etc/ssl/private

Unfortunately, it almost immediately stops running, and I see this in Logs within Portainer:

2019/08/21 17:14:03 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

The location /nginx/conf (folder) and /nginx/cert (folder) are created (by docker compose) on the host filesystem, but not files within them by nginx are being created. The D_DRIVE variable in my volume paths are used by many of my other containers without issues.

I have also tried using:
volumes:
- ${D_DRIVE}/nginx/nginx.conf:/etc/nginx/nginx.conf
- ${D_DRIVE}/nginx/conf.d:/etc/nginx/conf.d

(and creating empty files in the host location before-hand)...

In the latter case, it runs and exits due to:

nginx: [emerg] no "events" section in configuration

.. it doesn't actually populate the empty files I created with anything.

If I create an nginx container with no volume specified, it starts and keeps running... though I am unclear on how to actually edit nginx's config from outside the container, since I don't have editors within the console of the container... I also don't know how my change would 'persist' if I ended up re-creating the container.

I'm at a complete loss, and have tried multiple guides across the web that I've discovered... please, if anyone has thoughts on what process or guide I should follow, or what is causing the above issues based on my own deployment process... please help! Any guidance would be much appreciated.

Most helpful comment

When using a bind mounted folder (like ${D_DRIVE}/nginx/conf/:/etc/nginx/), it will not be filled in with anything from the image. If there are files in the image in the target folder (/etc/nginx/), they will be hidden and basically impossible to access in that container. In order to supply config to nginx, you need to provide a full config file on your host (and any other needed files like ssl certs) and then bind-mount the containing folders to their correct places in the container. You can get a copy of the initial nginx config from the container with the following (but you'll most likely need to customize it for your setup):

$ docker container create --name tmp1 nginx
$ # assuming D_DRIVE is set in the shell appropriately
$ mkdir -p ${D_DRIVE}/nginx/conf/
$ docker cp tmp1:/etc/nginx/conf.d/default.conf ${D_DRIVE}/nginx/conf/
$ docker container rm tmp1

Customize the config and then use this volume:

volumes:
- ${D_DRIVE}/nginx/conf:/etc/nginx/conf.d/

Exploring the container to find where to get/put stuff:

$ docker run -it --rm nginx bash
root@d1efe7bf95ce:/# ls -l /etc/nginx/
drwxr-xr-x 2 root root 4096 Aug 15 20:31 conf.d
-rw-r--r-- 1 root root 1007 Aug 13 08:50 fastcgi_params
-rw-r--r-- 1 root root 2837 Aug 13 08:50 koi-utf
-rw-r--r-- 1 root root 2223 Aug 13 08:50 koi-win
-rw-r--r-- 1 root root 5231 Aug 13 08:50 mime.types
lrwxrwxrwx 1 root root   22 Aug 13 08:50 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  643 Aug 13 08:50 nginx.conf
-rw-r--r-- 1 root root  636 Aug 13 08:50 scgi_params
-rw-r--r-- 1 root root  664 Aug 13 08:50 uwsgi_params
-rw-r--r-- 1 root root 3610 Aug 13 08:50 win-utf
root@d1efe7bf95ce:/# ls /etc/nginx/conf.d/
default.conf

All 5 comments

When using a bind mounted folder (like ${D_DRIVE}/nginx/conf/:/etc/nginx/), it will not be filled in with anything from the image. If there are files in the image in the target folder (/etc/nginx/), they will be hidden and basically impossible to access in that container. In order to supply config to nginx, you need to provide a full config file on your host (and any other needed files like ssl certs) and then bind-mount the containing folders to their correct places in the container. You can get a copy of the initial nginx config from the container with the following (but you'll most likely need to customize it for your setup):

$ docker container create --name tmp1 nginx
$ # assuming D_DRIVE is set in the shell appropriately
$ mkdir -p ${D_DRIVE}/nginx/conf/
$ docker cp tmp1:/etc/nginx/conf.d/default.conf ${D_DRIVE}/nginx/conf/
$ docker container rm tmp1

Customize the config and then use this volume:

volumes:
- ${D_DRIVE}/nginx/conf:/etc/nginx/conf.d/

Exploring the container to find where to get/put stuff:

$ docker run -it --rm nginx bash
root@d1efe7bf95ce:/# ls -l /etc/nginx/
drwxr-xr-x 2 root root 4096 Aug 15 20:31 conf.d
-rw-r--r-- 1 root root 1007 Aug 13 08:50 fastcgi_params
-rw-r--r-- 1 root root 2837 Aug 13 08:50 koi-utf
-rw-r--r-- 1 root root 2223 Aug 13 08:50 koi-win
-rw-r--r-- 1 root root 5231 Aug 13 08:50 mime.types
lrwxrwxrwx 1 root root   22 Aug 13 08:50 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  643 Aug 13 08:50 nginx.conf
-rw-r--r-- 1 root root  636 Aug 13 08:50 scgi_params
-rw-r--r-- 1 root root  664 Aug 13 08:50 uwsgi_params
-rw-r--r-- 1 root root 3610 Aug 13 08:50 win-utf
root@d1efe7bf95ce:/# ls /etc/nginx/conf.d/
default.conf

That worked perfectly, and thank you very much for the clarification on what was going on! Thank you again!!

Just wanted to comment that I found this after many days of pulling hair out because my nginx container wouldn't connect with an _empty_ configuration directory on the host machine! Thank you so much - there are many references around that advise mounting a volume to be able to change configuration files - none apart from this indicate that the host directory must contain them first!

I had experience the same thing. my gratitude to the gentleman posted the solution. your illustration was excellent. Thanks again !!

When Nginx container starts, it will try to find out nginx.conf which contains the information about the startup of Nginx.
The volume mapping will occur before that so the nginx.conf will be overridden and the directory will be empty hence the current error. Ideally, we prepare a custom conf file and then we map that to nginx.conf file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

colinmollenhour picture colinmollenhour  路  6Comments

ralphsmith80 picture ralphsmith80  路  5Comments

nottrobin picture nottrobin  路  6Comments

a-shink picture a-shink  路  3Comments

lrhazi picture lrhazi  路  3Comments