Transfering local files to docker container - docker COPY not updating with new files in directory

736 views Asked by At

I'm hosting my media-files from the same server as I host my Django application in. From time to time I need to add some more files for being served from the application, such as images.

I have set up so NGINX serves all my media files in a directory called /mediafiles inside my docker container, this is the same place where all images that a user upload will be located.

What I want to do is to add some files into my repo, and copy these files over to /mediafiles in the container, and be able to reach them trough the application (when run locally) or NGINX (when run in my server).

However, I'm running into some problems. I've tried with the following line in my Dockerfile:

COPY ./reponame/mediafiles $APP_HOME/mediafiles

Which works, but ONLY after I've run:

docker system prune --volumes 

Which isn't something I can do on the server, because this is where all the users uploaded images will be located. I know that this isn't the optimal set-up as you should use an external file storage for user-media, but this is what I can spend on my project at this time and haven't found a good storage solution for under $10 per month.

I've tried running the following commands to not having to do a full system prune, but not to any success - Docker don't recognize the added/removed files locally to update the container.

docker-compose up --build --force-recreate
docker-compose build --no-cache   
docker-compose up  --build
docker builder prune  

I also though about mounting my local "mediafiles" into the container in the docker-compose file, but this don't work and isn't something I understand is recommended due to what I understand is the conflict of overwriting files in the container, i.e. something like this:

- ./reponame/mediafiles:/home/kontorshund/web/mediafiles

So I'm a bit out of any ideas here on how to move forward in solving the issue, would very much appreciate someone with more understanding into the mechanisms of Docker on how to solve this issue.

Full Dockerfile

# pull official base image
FROM python:3.9.6

# set work directory
WORKDIR /usr/src/reponame

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# create the appropriate directories
RUN mkdir -p /home/reponame
ENV HOME=/home/reponame
ENV APP_HOME=/home/reponame/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
RUN mkdir $APP_HOME/mediafiles
WORKDIR $APP_HOME

COPY ./reponame/mediafiles $APP_HOME/mediafiles
COPY . $APP_HOME

Full docker-compose file

version: '3.8'

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/home/reponame/web
      - static_volume:/home/reponame/web/staticfiles
      - media_volume:/home/reponame/web/mediafiles
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev

volumes:
  static_volume:
  media_volume:
1

There are 1 answers

0
Jhnsbrst On BEST ANSWER

Okey, nevermind. Now when I'm trying the following it works fine, no idea what I did wrong the first time....,

version: '3.8'

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/home/reponame/web
      - static_volume:/home/reponame/web/staticfiles
      - ./mediafiles:/home/reponame/web/mediafiles
      #- media_volume:/home/reponame/web/mediafiles
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev

volumes:
  static_volume:
  media_volume: