10

This is my basic NGINX setup that works!

web:
  image: nginx
  volumes:
   - ./nginx:/etc/nginx/conf.d
  ....

I replace the volumes by copying ./nginx to /etc/nginx/conf.d using COPY ./nginx /etc/nginx/conf.d into my container. The issue was because, by using value the nginx.conf refer to log file in my host instead of my container. So, I thought by hardcopying the config file to container it will solve my problem.

However, NGINX is not running at all at docker compose up. What is wrong?

EDIT:

Dockerfile

FROM python:3-onbuild

COPY ./ /app
COPY ./nginx /etc/nginx/conf.d

RUN chmod +x /app/start_celerybeat.sh
RUN chmod +x /app/start_celeryd.sh
RUN chmod +x /app/start_web.sh

RUN pip install -r /app/requirements.txt
RUN python /app/manage.py collectstatic --noinput
RUN /app/automation/rm.sh

docker-compose.yml

version: "3"  
services:  
  nginx:
    image: nginx:latest
    container_name: nginx_airport
    ports:
      - "8080:8080"
  rabbit:
      image: rabbitmq:latest
      environment:
          - RABBITMQ_DEFAULT_USER=admin
          - RABBITMQ_DEFAULT_PASS=asdasdasd
      ports:
          - "5672:5672"
          - "15672:15672"
  web:
    build:
      context: ./
      dockerfile: Dockerfile
    command: /app/start_web.sh
    container_name: django_airport
    expose:
      - "8080"
    links:
      - rabbit
  celerybeat:
    build: ./
    command: /app/start_celerybeat.sh
    depends_on:
      - web
    links:
      - rabbit
  celeryd:
    build: ./ 
    command: /app/start_celeryd.sh
    depends_on:
      - web
    links:
      - rabbit
notalentgeek
  • 4,939
  • 11
  • 34
  • 53
  • If you have replaced the volumes tag with a COPY, you should paste your Dockerfile to give the full view of your problem. – lifeisfoo Oct 02 '17 at 11:37
  • @lifeisfoo, sorry I updated my answer. The Dockerfile is the Dockerfile for "web" services. It loads before NGINX start so I thought I put it there. – notalentgeek Oct 02 '17 at 11:50
  • Looks like you've mixed up `web` and `nginx`. If you want a custom image for `nginx` you need a separate `Dockerfile-nginx` for it. – schmunk Oct 02 '17 at 12:01
  • @schmunk, ya? Well because the NGINX only need `COPY ./nginx /etc/nginx/conf.d` I thought I can put in the same Dockerfile as web. Since the web is always first anyway. – notalentgeek Oct 02 '17 at 12:14
  • 1
    @notalentgeek if the answer solves your problem, please accept it. So future visitors can recognize it. – lifeisfoo Oct 08 '17 at 22:34

1 Answers1

9

This is your initial setup that works:

web:
  image: nginx
  volumes:
   - ./nginx:/etc/nginx/conf.d

Here you have a bind volume that proxy, inside your container, all file system requests at /etc/nginx/conf.d to your host ./nginx. So there is no copy, just a bind. This means that if you change a file in your ./nginx folder, you container will see the updated file in real time.

Load the configuration from the host

In your last setup just add a volume in the nginx service. You can also remove the COPY ./nginx /etc/nginx/conf.d line in you web service Dockerfile, because it's useless.

Bundle configuration inside the image

Instead, if you want to bundle your nginx configuration inside a nginx image you should build a custom nginx image. Create a Dockerfile.nginx file:

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

And then change your docker-compose:

version: "3"  
services:  
  nginx:
    build:
      dockerfile: Dockerfile.nginx
      container_name: nginx_airport
      ports:
        - "8080:8080"
# ...

Now your nginx container will have the configuration inside it and you don't need to use a volume.

lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
  • No, that is not what I meant. I want to put the NGINX configuration inside the container. NOT by mounting it, hence the `COPY`. However, with the `COPY`-ing the NGINX config it seems my NGINX in container does not run at all, albeit the mounting position and copy location are all the same. – notalentgeek Oct 02 '17 at 12:12
  • 3
    Containers have isolated file systems, so if you COPY the nginx config inside you web service image, the nginx container will be unable to "see" it. Only processes inside you web service container can see it. – lifeisfoo Oct 02 '17 at 12:17
  • What @lifeisfoo says in detail is the same I tried to say in my comment above. If you have the config on your `web` image, you could use `volumes_from: -web` in your nginx image, but I strongly recommend NOT to do that, as it create a lot of quirky side effects, when trying to update containers. – schmunk Oct 02 '17 at 13:23
  • You *can not* **copy** a file into a container with docker compose. The reason for this is because the maintainers found it shortsighted because the `COPY` operation would need to be repeated every time the container is created. E.g. running `docker-compose`. This is pretty contested and a sour spot for the community as you can see here https://github.com/docker/compose/issues/5523. Frankly I think the maintainers are "right" but... when I just wanted to copy an `nginx.conf` and now I need to create a separate `Dockerfile` at my projects root. meh. nbd I guess – Nick Brady Apr 27 '20 at 20:26
  • @lifeisfoo, i too have similar requirement where i copy my configurations to nginx image using COPY ./nginx /etc/nginx/conf.d.But it is failing with error "Service 'nginx' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder483163179/config/nginx/conf.d: no such file or directory". How can i solve this? my dockerfile is not in root directory but docker-compose is in root directory.Is this might be the cause? – Bharath Aug 30 '20 at 11:01