Copy static files to volume on deployment?

Viewed 1290

How can I ensure that the most recent static files created during deployment of a Django application are copied to a volume? My web container defines a Django application that, when built, runs python manage.py collectstatic --noinput --clear. This moves all static files into the /usr/src/app/static_files directory. I'd like these static files to become available in the staticdata volume such that nginx can serve them. In other words, the contents of /usr/src/app/static_files should overwrite the contents of existing files in the staticdata volume. Otherwise nginx will keep serving older copies of files such css files and images, instead of the updated files collected by Django.

Is this possible? It feels like I am using volumes wrong for this purpose, since their purpose is persisting data.

Edit1: Is there perhaps a way to create a "temporary volume" when the application is deployed, which is destroyed when the application is undeployed? If so, how do I do this, and how do I make this volume also available to nginx?

Edit2: If I am not mistaken an option would be to use a bind mount, but I've read that these should be avoided if possible such that an application is not dependent on the structure of the host's filesystem.

version: '3.3'

services:
  web:
    restart: always
    build: ./web
    env_file:
    - web-variables.env
    expose:
      - "8000"
    ports:
      - "8000:8000"
    volumes:
      - staticdata:/usr/src/app/static_files
      - staticdata:/usr/src/app/uploaded_files
    command: "./django-entrypoint.sh"
    depends_on:
      - database

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - staticdata:/data/www
    depends_on:
      - web

  database:
    image: postgres:9.2
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  staticdata:
  pgdata:
2 Answers

It seems the default location docker nginx image is looking at is /usr/share/nginx/html and not /data/www as you wanted it.

So just change this line and it should work:

 nginx:
    ...
      - staticdata:/usr/share/nginx/html
    ...
Related