Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy static files to volume on deployment?

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:
like image 208
Sam Avatar asked Sep 18 '17 11:09

Sam


1 Answers

Yes, it's possible - you were nearly there. By passing the option --clear to collectstatic, it will clear the directory before collecting the static files. That's not the behaviour you want. Simply run it without that flag:

python manage.py collectstatic --noinput

And it should only copy over newer static files which don't exist at the destination yet.

Looking forward, you might want to consider having separate containers for static files and media files (user-generated content) as things could get confusing otherwise as seen here :)

like image 199
olieidel Avatar answered Sep 28 '22 06:09

olieidel