简体   繁体   English

在“docker-compose run”命令(Django的collectstatic)之后,卷更改不会持久

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

I have a Django environment that I create with Docker Compose, and I'm trying to use manage.py collectstatic to copy my site's static files to a directory in the container. 我有一个用Docker Compose创建的Django环境,我正在尝试使用manage.py collectstatic将我站点的静态文件复制到容器中的目录。 This directory (/usr/src/app/static) is also a Docker Volume. 这个目录(/ usr / src / app / static)也是一个Docker Volume。

After building my docker containers ( docker-compose build ), I run docker-compose run web python manage.py collectstatic , which works as expected, but my web server (Nginx) is not finding the files, nor are there any files when I run docker-compose run web ls -la /usr/src/app/static . 在构建我的docker容器( docker-compose build )之后,我运行了docker-compose run web python manage.py collectstatic ,它按预期工作,但我的Web服务器(Nginx)找不到文件,当我没有任何文件时运行docker-compose run web ls -la /usr/src/app/static

Any ideas on what I'm doing wrong? 关于我做错了什么的任何想法?

(Note: I don't have manage.py collectstatic in my Dockerfile because my setup needs my ".env" file loaded, and I didn't see a way to load this in the Dockerfile. In either case, I would like to know why Docker Compose doesn't work as I'm expecting it to.) (注意:我的Dockerfile中没有manage.py collectstatic ,因为我的设置需要加载我的“.env”文件,我没有看到在Dockerfile中加载它的方法。在任何一种情况下,我都想知道为什么Docker Compose不起作用,因为我期待它。)

Here are my config files: 这是我的配置文件:

## docker-compose.yml:
web:
  restart: always
  build: .
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - /usr/src/app/static
    - .:/code
  env_file: .env
  command: /usr/local/bin/gunicorn myapp.wsgi:application -w 2 -b :8000 --reload

nginx:
  restart: always
  build: ./config/nginx
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

postgres:
  restart: always
  image: postgres:latest
  volumes:
    - /var/lib/postgresql
  ports:
    - "5432:5432"


## Dockerfile:
FROM python:3.4.3
RUN mkdir /code
WORKDIR /code
ADD . /requirements/ /code/requirements/
RUN pip install -r /code/requirements/docker.txt
ADD . /code/

Running docker-compose run ... starts a new container and executes the command in there. 运行docker-compose run ...启动一个新容器并在那里执行命令。 then when you run docker-compose up it creates ANOTHER new container... which doesn't have the changes from your previous command. 然后当你运行docker-compose up它会创建另一个新的容器...它没有你上一个命令的更改。

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 并将web-static添加到nginx容器的'volumes-from'列表中

There are a couple of other ways to do this in addition to Paul Becotte's method: 除了Paul Becotte的方法之外,还有其他几种方法可以做到这一点:

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 答:随着docker-compose v 1.6的发布(在Paul回答时不可用),您现在可以使用docker-compose文件版本2来指定卷

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. 使用此方法应该比Pauls的方法涉及更少的系统开销,因为您运行的容器少了一个。

B. Slight hack - you can collect static files in the container command B.轻微破解 - 您可以在容器命令中收集静态文件

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. 这样做的缺点是,如果你从docker-compose命令行调用另一个命令,那么它就不灵活了,那么collectstatic将无法运行。 Also you are running collectstatic files at times when you probably don't need to. 此外,当您可能不需要时,您正在运行collectstatic文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM