notalentgeek
notalentgeek

Reputation: 5839

Docker + NGINX, how can I copy the configuration file from host to container?

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

Upvotes: 13

Views: 34724

Answers (1)

lifeisfoo
lifeisfoo

Reputation: 16374

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.

Upvotes: 11

Related Questions