Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

nginx - Confusion on using docker-compose volumes to serve django static files

Docker rookie here, trying to setup a simple Django project using Compose. I've had success with this in the past, but I'm trying a different setup this time, which I can't figure out why it doesn't work.

I have the following docker-compose.yml file:

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

My /Dockerfile is:

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

My /docker/web/Dockerfile is:

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

And my /docker/web/default.conf file is:

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;
    }
}

The output from docker shows the static files installing into /static_files, but nginx returns 404 for all the files under /static. If I look in my .static folder (in root of project), the directory is empty (aside from a .gitkeep file I have in there). If I run docker-compose app run ls -la /static_files, the directory is empty, but docker-compose app run ls -la /app/.static has the .gitkeep file. Clearly I'm misunderstanding something with Docker and Compose. Any thoughts on what I'm doing wrong? My understanding is that the RUN SECRET_KEY=tmpkey python manage.py collectstatic --noinput should be writing files to my local .static folder and that nginx should see these files; neither is happening.

Software versions: docker-compose version 1.7.0, build 0d7bf73 and Docker version 1.11.0, build 4dc5990 on OS X, with docker-machine connected to cloud instance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am still unclear of why my original code does not work, but switching my code to use Compose's v2 format works, where I have my volumes defined outside of services.

Here's my updated docker-compose.yml file:

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

The rest of the config files remained the same.

(It may be worth noting that before I ran this new config, I deleted all existing Docker volumes listed in docker volume ls -- perhaps this was my actual fix?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...