Docker Community Forums

Share and learn in the Docker community.

Nginx in docker - [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

Hi,
I have a problem on my basic configuration with my environment with nginx,mysql,wordpress,mediawiki (local on Windows 10 PC). I use docker-compose to build the environment, and all is done, but after e few seconds the nginx container go down with the status Exited (1) 12 seconds ago . My configuration is really basic and I don’t understand why it doesn’t works… This is my nginx config:

events { worker_connections 1024; }
http {
    # this part is www redirect
    server {
      server_name blog;
      listen 80;
      #return 301 http://blog$request_uri;
      location / {
            proxy_set_header   Host $host;
            proxy_pass http://blog:80;
        }
    }
    server {
      server_name wiki;
      listen 80;
      #return 301 http://wiki$request_uri;
      location / {
            proxy_set_header   Host $host;
            proxy_pass http://wiki:80;
        }
    }
}

And this is my compose file:

version: "3.7"
services:
  site-db:
    image: mysql:5.7
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      - site-net
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    restart: always
    ports: 
      - 3306:3306
  nginx:
    build:
      context: .
      target: runtime-nginx
      dockerfile: nginx/Dockerfile
    image: nginx
    depends_on:
      - blog
      - wiki
    networks:
      - site-net
    ports:
      - "80:80"
    volumes:
      - ./:/etc/nginx
  blog:
    depends_on:
       - site-db
    image: wordpress
    networks:
      - site-net
    restart: always
    environment:
      WORDPRESS_DB_NAME: blog
      WORDPRESS_DB_HOST: site-db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    working_dir: /var/www/html
    volumes:
      - ./blog:/var/www/html
  wiki:
    depends_on:
       - site-db
    image: mediawiki
    networks:
      - site-net
    restart: always
    environment:
      WORDPRESS_DB_NAME: wiki
      WORDPRESS_DB_HOST: site-db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    working_dir: /var/www/html
    volumes:
      - ./wiki:/var/www/html
networks:
  site-net:
    driver: bridge

What can be wrong in this basic config? Thank you very much for the help.

This is the answer to docker-compose up :

docker-compose up
Creating network "compose_site-net" with driver "bridge"
Creating compose_site-db_1 ... done
Creating compose_wiki_1    ... done
Creating compose_blog_1    ... done
Creating compose_nginx_1   ... done
Attaching to compose_site-db_1, compose_wiki_1, compose_blog_1, compose_nginx_1
site-db_1  | 2021-01-14T18:17:55.523382Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
site-db_1  | 2021-01-14T18:17:55.524942Z 0 [Note] mysqld (mysqld 5.7.27) starting as process 1 ...
site-db_1  | 2021-01-14T18:17:55.528517Z 0 [Note] InnoDB: PUNCH HOLE support available
site-db_1  | 2021-01-14T18:17:55.528533Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
site-db_1  | 2021-01-14T18:17:55.528536Z 0 [Note] InnoDB: Uses event mutexes
site-db_1  | 2021-01-14T18:17:55.528539Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
site-db_1  | 2021-01-14T18:17:55.528541Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
site-db_1  | 2021-01-14T18:17:55.528543Z 0 [Note] InnoDB: Using Linux native AIO
site-db_1  | 2021-01-14T18:17:55.528718Z 0 [Note] InnoDB: Number of pools: 1
site-db_1  | 2021-01-14T18:17:55.528842Z 0 [Note] InnoDB: Using CPU crc32 instructions
site-db_1  | 2021-01-14T18:17:55.570122Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
site-db_1  | 2021-01-14T18:17:55.581655Z 0 [Note] InnoDB: Completed initialization of buffer pool
site-db_1  | 2021-01-14T18:17:55.584441Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
site-db_1  | 2021-01-14T18:17:55.596567Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
site-db_1  | 2021-01-14T18:17:55.604009Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
site-db_1  | 2021-01-14T18:17:55.604046Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
site-db_1  | 2021-01-14T18:17:55.607357Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
site-db_1  | 2021-01-14T18:17:55.608113Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
site-db_1  | 2021-01-14T18:17:55.608149Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
site-db_1  | 2021-01-14T18:17:55.608622Z 0 [Note] InnoDB: Waiting for purge to start
site-db_1  | 2021-01-14T18:17:55.687027Z 0 [Note] InnoDB: 5.7.27 started; log sequence number 12464561
site-db_1  | 2021-01-14T18:17:55.687278Z 0 [Note] Plugin 'FEDERATED' is disabled.
site-db_1  | 2021-01-14T18:17:55.691199Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
site-db_1  | 2021-01-14T18:17:55.691364Z 0 [Warning] CA certificate ca.pem is self signed.
site-db_1  | 2021-01-14T18:17:55.692346Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
site-db_1  | 2021-01-14T18:17:55.694824Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210114 18:17:55
site-db_1  | 2021-01-14T18:17:55.695673Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
site-db_1  | 2021-01-14T18:17:55.695700Z 0 [Note] IPv6 is available.
site-db_1  | 2021-01-14T18:17:55.695705Z 0 [Note]   - '::' resolves to '::';
site-db_1  | 2021-01-14T18:17:55.695714Z 0 [Note] Server socket created on IP: '::'.
site-db_1  | 2021-01-14T18:17:55.699406Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
site-db_1  | 2021-01-14T18:17:55.713247Z 0 [Note] Event Scheduler: Loaded 0 events
site-db_1  | 2021-01-14T18:17:55.713368Z 0 [Note] mysqld: ready for connections.
site-db_1  | Version: '5.7.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
wiki_1     | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.3. Set the 'ServerName' directive globally to suppress this message
wiki_1     | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.3. Set the 'ServerName' directive globally to suppress this message
wiki_1     | [Thu Jan 14 18:17:56.652985 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.2.21 configured -- resuming normal operations
wiki_1     | [Thu Jan 14 18:17:56.653333 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
blog_1     | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.4. Set the 'ServerName' directive globally to suppress this message
blog_1     | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.4. Set the 'ServerName' directive globally to suppress this message
blog_1     | [Thu Jan 14 18:17:56.996511 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.3.8 configured -- resuming normal operations
blog_1     | [Thu Jan 14 18:17:56.996553 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
nginx_1    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1    | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
nginx_1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1    | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1    | 2021/01/14 18:17:57 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx_1    | nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

So what can I do? the file exists in the directory (where i run the powershell)…

Output of the directory from where i launch the powershell :

ls


    Directory: C:\Users\XXX\Documents\DOCKER\COMPOSE


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       12/01/2021     16:57                nginx
-a----       14/01/2021     20:39           1350 docker-compose.yml
-a----       05/01/2021     15:26            525 nginx.conf

The nginx.conf is both in the root and in the nginx folder.

The nginx container works if I delete the volumes section, but at that point… how can I put the config file in the container?

Thanks for the support!

 it seems that the reason is commend of "volumes" in docker-compose.yml will delete the container's file which you want to mount out as ones the nginx running but can't find  nginx.conf so it will throw error.
to fixed this problem you can refrence the official docker images which publiched in hub.docker.com.

refrence:Docker Hub

Complex configuration

$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx

If you wish to adapt the default configuration, use something like the following to copy it from a running nginx container:

$ docker run --name tmp-nginx-container -d nginx
$ docker cp tmp-nginx-container:/etc/nginx/nginx.conf /host/path/nginx.conf
$ docker rm -f tmp-nginx-container

This can also be accomplished more cleanly using a simple Dockerfile (in /host/path/ ):

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf