Volume changes not persistent after “docker-compose run” command (Django's collectstatic)

吃可爱长大的小学妹 提交于 2019-12-05 17:27:57

Running docker-compose run ... starts a new container and executes the command in there. then when you run docker-compose up it creates ANOTHER new container... which doesn't have the changes from your previous command.

What you want to do is start up a data container to hold your static files. Add another container to your compose file like this...

web-static:
  build: .
  volumes:
    - /usr/src/app/static
  env_file: .env
  command: manage.py collectstatic

and add web-static to the 'volumes-from' list on your nginx container

There are a couple of other ways to do this in addition to Paul Becotte's method:

A. With the release of docker-compose v 1.6 (not available at the time of Paul's answer) you can now use docker-compose file version 2 for specifying volumes

version: '2'
volumes:
  django-static:
    driver: local

django:
  ...
  volumes:
    - django-static:/usr/src/app/static

then you can collect static files in a separate container and they will persist

docker-compose run django ./manage.py collectstatic

Using this method should involve less system overhead then Pauls' method because you are running one less container.

B. Slight hack - you can collect static files in the container command

django:
  command: bash -c "./manage.py collectstatic --noinput;
  /usr/local/bin/gunicorn myapp.wsgi:application -w 2 -b :8000 --reload"

Downside of this is that it's inflexible if you are calling a different command from docker-compose command line then collectstatic will not get run. Also you are running collectstatic files at times when you probably don't need to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!