使用Docker的django,nginx和gunicorn应用程序的静态文件?

时间:2019-03-18 12:08:20

标签: django docker docker-compose

我无法获取静态文件。我尝试了各种设置和目录配置等,但是它们只是显示为404。

应用程序的文件夹结构如下

.
├── config
│   └── nginx
│       └── conf.d
│           └── local.conf
├── docker-compose.yml
├── Dockerfile
├── aggre
│   ├── aggre
│   └── manage.py
|   └── static
├── requirements.txt

Settings.py看起来像

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR),'static']
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static')

Dockerfile

FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/django/src
WORKDIR /opt/services/django/src
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/
COPY . /opt/services/django/src
RUN python aggre/manage.py collectstatic --no-input  # <-- here
CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
EXPOSE 8000
ADD . /opt/services/django/src
CMD ["gunicorn", "--chdir", "aggre", "--bind", ":8000", "aggre.wsgi:application"]

docker-compose.yml

version: '2'
services:
  djangoapp:
    build: .
    volumes:
      - .:/opt/services/django/src
      - static_volume:/opt/services/django/static/
    networks:
      - nginx_network

  nginx:
    image: nginx:1.13
    ports:
      - "80:80"
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static_volume:/opt/services/django/static/ 
    depends_on:
      - djangoapp
    networks:
      - nginx_network

networks:
  nginx_network:
    driver: bridge

volumes:
  static_volume: 

local.conf

server {
    listen 80;
    server_name ***.***.io;

    location / {
        proxy_pass http://djangoapp:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    location /static {
        alias /opt/services/django/static;
    }
}

我正在使用 **

  

docker-compose up --built

应用程序已成功启动,但未加载静态文件。 我正在

  

“ / opt / services / django / src / static / js / bootstrap.js”失败(2:没有   文件或目录)   **

我无法找到无法加载静态文件的地方。

0 个答案:

没有答案