0

Im running a docker container where i copy the content of current folder in /app in the container.

Then I put in a volume /app/media folder of the container.

However, when the volume is already created from a previous docker-compose build, i dont find all the new files put in my ./media folder, supposed to be copied to /app/media in the container...

Therefore i'm wondering how docker is populating the volume ? Is it not supposed to check in the container folder new files and put them in the volume?

I had the issue first and it was /media folder in the .dockerignore file, but now it's doing this again with other files in /media folder

Following What is the right way to add data to an existing named volume in Docker? I ve tried to do :

docker run -v mediafiles:/data --name helper busybox true
cd ./media && docker cp . helper:/data
docker rm helper

And it is now working

Thank you

Here is my docker-compose.yml

version: '3.7'

services:
  nginx:
    build:
      context: .
      dockerfile: ./compose/production/nginx/Dockerfile
    restart: always
    ports:
      - 80:80
    depends_on:
      - backend
      - frontend
    volumes:
      - staticfiles:/app/static
      - mediafiles:/app/media
    networks:
      spa_network:
  frontend:
    build:
      context: .
      dockerfile: ./compose/production/frontend/Dockerfile
    restart: always
    stdin_open: true
    command: yarn start
    ports:
      - 3000:3000
    depends_on:
      - backend
    networks:
      spa_network:
        ipv4_address: 172.20.128.3
  backend:
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    restart: always
    command: /start
    volumes:
      - staticfiles:/app/static
      - mediafiles:/app/media
      - sqlite_db:/app/db  
    ports:
      - 8000:8000
    env_file:
      - ./env/prod-sample
    networks:
      spa_network:
        ipv4_address: 172.20.128.2

networks:
  spa_network:
    ipam:
      config:
        - subnet: 172.20.0.0/16

volumes:
  sqlite_db:
  staticfiles:
  mediafiles:

Here is my dockerfile for backend (where i dont find the /app/media files)

FROM python:3.8-slim-buster

ENV PYTHONUNBUFFERED 1

RUN apt-get update \
  # dependencies for building Python packages
  && apt-get install -y build-essential netcat \
  # psycopg2 dependencies
  && apt-get install -y libpq-dev \
  # Translations dependencies
  && apt-get install -y gettext \
  # cleaning up unused files
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

RUN addgroup --system django \
    && adduser --system --ingroup django django

# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

#COPY ./compose/production/django/entrypoint /entrypoint
#RUN sed -i 's/\r$//g' /entrypoint
#RUN chmod +x /entrypoint
#RUN chown django /entrypoint

COPY ./compose/production/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
RUN chown django /start

WORKDIR /app

# avoid 'permission denied' error

# copy project code
COPY . .

RUN chown -R django:django /app


#USER django
#ENTRYPOINT ["/entrypoint"]

1 Answers1

0

If the volume is new, then docker will copy any files in the image to the new volume. If the volume isn't new, then nothing is copied and you get the existing, old, contents of the volume.

More info here: https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • 1
    I see. Then How can I copy new files to old volume? –  Jun 15 '22 at 14:19
  • You can delete the volume before you start so it's always new. Or you can create a startup script that copies the files. – Hans Kilian Jun 15 '22 at 14:23
  • i've found this https://stackoverflow.com/questions/37468788/what-is-the-right-way-to-add-data-to-an-existing-named-volume-in-docker i'm testing it –  Jun 15 '22 at 15:02
  • it's working correctly –  Jun 15 '22 at 16:44