Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 868
notalentgeek Avatar asked Oct 02 '17 11:10

notalentgeek


People also ask

Where is the nginx config file in Docker?

Maintaining Content and Configuration Files on the Docker Host. Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container.

How do I transfer files from one container to another?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).


1 Answers

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.

like image 190
lifeisfoo Avatar answered Sep 29 '22 12:09

lifeisfoo