ImanGM
ImanGM

Reputation: 89

Docker: Nginx doesn't run if I declare volumes in docker-compose.yml

I'm trying to run nginx as a container in docker using docker-compose but unfortunately, I'm not able to run it properly.

Here is my docker-compose.yml:

version: '3'
services:
 webserver:
  container_name: webserver
  hostname: webserver
  image: nginx
  ports:
   - 80:80
   - 443:443
  volumes:
   - ./nginx:/etc/nginx

and here is the error:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/01/18 19:04:26 [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)

I've used both relative and absolute paths in volumes but none of them worked. If I have the directory available on the host, it won't work. If I don't have the directory in the host, when I run docker-compose up, it will create an empty directory for nginx in the host but it will be left empty.

Any ideas what's wrong with my setup?

Thank you.

Upvotes: 2

Views: 2207

Answers (1)

Dr Claw
Dr Claw

Reputation: 686

No don't try to modify all the confs manually in the containers itself.

Nginx have /etc/nginx/conf.d for that so mount your customs confs inside.

Example:

You current directory shouldl look like this:

.
├── conf
│   └── custom.conf
├── docker-compose.yml
└── html
    └── index.html

docker-compose.yml

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./conf:/etc/nginx/conf.d # custom conf goes here
      - ./html:/tmp              # custom html goes here

I just put the html inside "/tmp" for showing you that my custom config works ..

./conf/custom.conf

server {
    listen     80;
    location / {
            root /tmp/;
            index  index.html index.htm;
    }
}

./html/index.html

<h1>nginx custom conf</h1>

Then

$ docker-compose up -d
Creating network "nginx_default" with the default driver
Creating nginx_nginx_1 ... done

$ curl localhost
<h1>nginx custom conf</h1>

Upvotes: 2

Related Questions