Compose: Nginx container doesn鈥檛 copy files to my host

Created on 19 Jun 2020  路  2Comments  路  Source: docker/compose

I am trying to copy a set of files (etc/nginx/*) from Nginx container to myhost (./nginx) but don't work.
Every time I run my docker-compose, the folder on the host (/ usr / development / nginx) is empty. How can I do this?

Environment
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

Docker Compose
docker-compose version 1.25.5, build 8a1c60f6

Docker
Docker version 19.03.6, build 369ce74a3c

My docker-compose.yml
`version: '3.7'

services:
  nginx:
    container_name: 'nginx'
    hostname: nginx
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    restart: unless-stopped
    command: /bin/sh -c "while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g 'daemon off;'"
    volumes:
      - ./nginx:/etc/nginx
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
    networks:
      - docker-network
  portainer:
    image: portainer/portainer
    container_name: portainer
    hostname: portainer
    ports:
     - 9000:9000
    volumes:
     - /var/run/docker.sock:/var/run/docker.sock
     - ./portainer/data:/data
    networks:
      - docker-network

  certbot:
    container_name: 'certbot'
    hostname: certbot
    image: certbot/certbot
    volumes:
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
    restart: unless-stopped
    entrypoint: /bin/sh -c "trap exit TERM; while :; do certbot renew; sleep 12h & wait ${!}; done;"
    depends_on:
      - nginx
    networks:
      - docker-network
networks:
  docker-network:
    name: docker-network
    driver: bridge`
kinbug

All 2 comments

You should use a COPY directive in a your Dockerfile, if you want build an image to delivery for: AWS,
Docker Hub, etc. Here are you using bindings for your volumes:

volumes:
  - ./nginx:/etc/nginx
  - ./certbot/conf:/etc/letsencrypt
  - ./certbot/www:/var/www/certbot

When you build an image then you have no files in your builded image, because you haven't copied them. For copying the files, you need to add your own Dockerfile for building the nginx service and set a context for building.

Look at my docker-compose-staging-build.yml for nginx image building:

./docker-compose-staging-build.yml

nginx:
    build: 
      context: ./  <-- [Relative path to your docker-compose.yml]
      dockerfile: ./docker/configs/staging/nginx/Dockerfile  <-- [Directory with a Dockerfile to build image]
    image: company/nginx:staging  <-- [Your repository and image name with a tag(staging) for pushing to Docker Hub]
    container_name: "nginx"

./docker/configs/staging/nginx/Dockerfile

FROM nginx:stable-alpine

USER root:root
WORKDIR /root

RUN apk update && apk upgrade

COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx /etc/nginx  <-- [There is your copying instruction to image, directory may be different]

EXPOSE 80 443
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]  <-- [You can use this command instead of /bin/sh -c "while :; do sleep 6h...'" in you docker-compose.yml]

Compose files for running and building should be different!
You need to create some files for development, production environments, e.g: dc-develop-build.yml, dc-product-build.yml etc. Use a -f flag when you run docker-compose build to pass a docker-compose file name.

Best regards.

@GenaANTG Thanks!! Got it!!

Was this page helpful?
0 / 5 - 0 ratings