Bram Janssen
Bram Janssen

Reputation: 1327

docker nginx - doen't go up when trying to add volumes for nginx.conf / default.conf

When I try to run a nginx image with volumes to copy/sync "nginx.conf" and the "conf.d folder" it doesn't spin up. No errors, but if I run docker container ls, the container is not listed. If I comment the "Volumes with issues" it does spin up the container and all works fine. I want to note that if I comment either one of them out the container doesn't spin up, the problem lies in both volumes.

Folder structure:

Project Name
|-- core_dir
|-- core_dir
|-- core_file.file
|-- core_file.file
|-- docker-compose.yml
`-- docker
    |-- php
    |   |-- Dockerfile
    |   `-- php.ini
    |-- mysql
    `-- nginx
        |-- nginx.conf
        `-- conf.d
            `-- default.conf

Volumes with issue:

volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

My docker-compose.yml:

version: "3.0"

services:

  #nginx
  nginx:
    image: nginx:stable-alpine
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php
      - mysql
    networks:
      - wordpress


  #mysql
  mysql:
    image: mysql:5.7.22
    env_file:
      - .env.development
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE:
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
    networks:
      - wordpress
    volumes:
      - ./docker/mysql:/var/lib/mysql

  #php
  php:
    build:
      context: ./docker/php
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./:/var/www/html
      - ./docker/php/php.ini:/usr/local/etc/php/php.ini
    ports:
      - "9000:9000"
    networks:
      - wordpress

networks:
  wordpress:

Thanks in advance,

Bram

Upvotes: 1

Views: 7070

Answers (3)

Bram Janssen
Bram Janssen

Reputation: 1327

I have fixed the problem.

Thanks to rok who pointed out to me that I can check the logs of a crashed container I was able top see what error caused the crash.

Check logs for failed container

run docker ps -a to see the failed containers and then run docker logs [container_id] on the failed container.

Error

"daemon" directive is duplicate in /etc/nginx/nginx.conf:

Nginx.conf

 user  nginx;
    worker_processes  4;
    daemon off;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        client_max_body_size 100M;
        server_names_hash_bucket_size 64;

        access_log  /var/log/nginx/access.log;
        # Switch logging to console out to view via Docker
        #access_log /dev/stdout;
        #error_log /dev/stderr;

        sendfile        on;
        keepalive_timeout  65;

        include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-available/*.conf;
    }

Solution

Removing the line "daemon off;" fixed the problem for me.

Upvotes: 3

Satish Kumar Nadarajan
Satish Kumar Nadarajan

Reputation: 196

Often a times the issue's with usage of relative path in compose file.

Could you try using absolute path or named volumes instead of using relative path.

Upvotes: 0

rokpoto.com
rokpoto.com

Reputation: 10778

There may be many reasons why a container fails:

  • configuration you mount to container is faulty
  • folders you mount doesn't exit on the host

To understand what's wrong you need to run docker ps -a to see the failed containers and then run docker logs [container_id] on the failed container.

Moreover, you can follow the docs of nginx and try to run nginx only with Docker.

To rule out the issues with volumes, you can build a custom nginx image with configurations:

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/nginx /etc/nginx

docker build -t mynginx_image1 .

docker run --name mynginx3 -p 80:80 -d mynginx_image1

Upvotes: 2

Related Questions