首页 文章

混淆使用docker-compose卷来提供django静态文件

提问于
浏览
1

Docker新手在这里尝试使用Compose设置一个简单的Django项目 . 我已经success with this in the past,但我想知道它为什么不起作用 .

我有以下docker-compose.yml文件:

data:
  image: postgres:9.4
  volumes:
    - /var/lib/postgresql
  command: /bin/true

db:
  image: postgres:9.4
  restart: always
  expose:
    - "5432"
  volumes_from:
    - data

app:
  build: .
  restart: always
  env_file: .env
  expose:
    - "8000"
  links:
    - db:db
  volumes:
    - .static:/static_files

web:
  build: docker/web
  restart: always
  ports:
    - "80:80"
  links:
    - app:app
  volumes_from:
    - app

我/ Dockerfile是:

FROM python:3.5
ENV PYTHONUNBUFFERED 1

ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
RUN SECRET_KEY=tmpkey python manage.py collectstatic --noinput

CMD gunicorn mysite.wsgi:application -b 0.0.0.0:8000

我的/ docker / web / Dockerfile是:

FROM nginx:1.9
RUN rm /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/

我的/docker/web/default.conf文件是:

server {
    listen 80 default_server;

    location /static/ {
      autoindex on;
      alias /static_files/;
    }

    location / {
        proxy_pass http://app:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

docker的输出显示安装到/ static_files中的静态文件,但nginx为/ static下的所有文件返回404 . 如果我查看我的.static文件夹(在项目的根目录中),该目录为空(除了我在那里的.gitkeep文件) . 如果我运行 docker-compose app run ls -la /static_files ,则目录为空,但 docker-compose app run ls -la /app/.static 具有.gitkeep文件 . 显然我'm misunderstanding something with Docker and Compose. Any thoughts on what I'我做错了吗?我的理解是 RUN SECRET_KEY=tmpkey python manage.py collectstatic --noinput 应该将文件写入我的本地.static文件夹,并且nginx应该看到这些文件;都没有发生 .

软件版本:docker-compose版本1.7.0,build 0d7bf73和Docker版本1.11.0,在OS X上构建4dc5990,docker-machine连接到 Cloud 实例 .

1 回答

  • 4

    我仍然不清楚为什么我的原始代码不起作用,但是切换我的代码以使用Compose的v2格式工作,我在服务之外定义了我的卷 .

    这是我更新的docker-compose.yml文件:

    version: '2'
    services:
      db:
        image: postgres:9.4
        restart: always
        expose:
          - "5432"
        volumes:
          - postgres-data:/var/lib/postgresql
      app:
        build: .
        restart: always
        env_file: .env
        expose:
          - "8000"
        links:
          - db:db
        volumes:
          - static-data:/static_files
      web:
        build: docker/web
        restart: always
        ports:
          - "80:80"
        links:
          - app:app
        volumes:
          - static-data:/static_files
    volumes:
      postgres-data:
        driver: local
      static-data:
        driver: local
    

    其余的配置文件保持不变 .

    (可能值得注意的是,在我运行这个新配置之前,我删除了_2742758中列出的所有现有Docker卷 - 也许这是我的实际修复?)

相关问题