带有docker compose的Django和nginx - 未加载静态文件

时间:2016-11-02 14:57:44

标签: django nginx docker docker-compose

我知道有几个类似的问题已被提出,但我无法在任何地方找到合适的答案。

我想将我的Django应用程序停靠。该应用程序在docker容器中工作正常,但未加载静态文件(图像,css等)。

我的docker-compose.yml

version: '2' 
services:   
  web:
    build: .
    command: ./start.sh
    volumes:
      - .:/app
      - /app/www/static
    ports:
      - "8000:8000"
    env_file: .env

  nginx:
    build: ./nginx
    links:
      - web
    ports:
      - "0.0.0.0:80:80"
    volumes_from:
      - web

start.sh脚本

python manage.py makemigrations --noinput
python manage.py migrate --noinput
python manage.py collectstatic --noinput

exec gunicorn audiobridge.wsgi:application --bind 0.0.0.0:8000 --workers 3

Dockerfile容器的web

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

然后我的项目中有一个nginx /目录,其中包含以下Dockerfile

FROM nginx
COPY sites-enabled/audiobridge /etc/nginx/sites-enabled/

并且在nginx / sites-enabled内部有nginx配置文件:

server {

listen 80;
server_name 127.0.0.1;
charset utf-8;

location /static {
    alias /app/www/static/;
}

location / {
    proxy_pass http://web: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;
}
}

1 个答案:

答案 0 :(得分:0)

enter image description here

卷:       -“ ../nginx/conf.d:/etc/nginx/conf.d/templates”       -“ / static:/ static” 您是否尝试过将此代码添加到nginx和django应用程序中?