我是Django和Docker开发的新手。我正在创建一个小项目,以掌握其使用中的一些知识。但是,我无法加载静态文件(css,javascript)。我现在正在使用一些配置:
docker-compose.yml
version: '3'
services:
# database containers, one for each db
database1:
image: postgres:10
volumes:
- database1_volume:/var/lib/postgresql/data
env_file:
- config/db/database1_env
networks:
- database1_network
# web container, with django + gunicorn
djangoapp:
build: .
restart: on-failure
volumes:
- .:/opt/services/djangoapp/
- static:/opt/services/djangoapp/app/static
networks:
- database1_network
- nginx_network
depends_on:
- database1
expose:
- "8000"
# reverse proxy container (nginx)
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static:/opt/services/djangoapp/app/static
networks:
- nginx_network
depends_on:
- djangoapp
networks:
database1_network:
driver: bridge
nginx_network:
driver: bridge
volumes:
database1_volume:
static:
media:
dockerfile
FROM python:3.6
#RUN pip install pipenv && pipenv install --system
RUN mkdir -p /opt/services/djangoapp/
WORKDIR /opt/services/djangoapp/
COPY requirements.txt /opt/services/djangoapp/
RUN pip3 install -r /opt/services/djangoapp/requirements.txt
COPY . /opt/services/djangoapp/
RUN cd app && python manage.py collectstatic --noinput
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "app", "app.wsgi:application"]
nginx local.conf
upstream app_server {
server djangoapp:8000;
}
server {
listen 80;
server_name localhost;
location /static/ {
autoindex on;
alias /opt/services/djangoapp/app/static;
}
location / {
proxy_pass http://app_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
setting.py
STATIC_URL = '/static/'
STATIC_ROOT = '/opt/services/djangoapp/app/static'
我已经尝试了许多不同的方法来解决此问题。我已经在容器内进行了静电收集。将setting.py更改为许多不同的路径。已授予用户权限。
我将不胜感激并看到帮助。
谢谢