我正面临一个已经耗费了我大量时间的问题。我正尝试使用 docker 和 django 部署一个应用程序,但一旦提示找不到静态文件,我就无法读取它。我进入了容器,文件就在它们应该在的地方。
我将在下面添加一些代码:
Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /www
COPY Pipfile Pipfile.lock /www/
WORKDIR /www
RUN pip install pipenv && pipenv install --system
COPY . /www/
RUN pip install -r requirements.txt
RUN cd myapp-root && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "myapp-root", "myapp.wsgi:application"]
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
docker-compose.yml
version: '3'
services:
# database containers, one for each db
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- db_volume:/var/lib/postgresql/data/
# web container, with django + gunicorn
djangoapp:
build: .
environment:
- DJANGO_SETTINGS_MODULE
volumes:
- .:/www
links:
- postgres:postgres
- redis:redis
env_file: .env
# reverse proxy container (nginx)
nginx:
image: nginx:1.13
ports:
- 80:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- ./myapp-root/static:/static
- ./myapp-root/media:/media
depends_on:
- djangoapp
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
volumes:
- redisdata:/data
volumes:
db_volume:
redisdata:
本地.conf(nginx)
server {
listen 80;
server_name localhost;
location /static/ {
alias /www/myapp-root/static/;
}
location /media/ {
alias /www/myapp-root/media/;
}
location / {
proxy_pass http://djangoapp;
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://djangoapp;
break;
}
}
}
我已多次更改文件的非本地化,但仍然无法在 docker 容器中找到它。
nginx_1 | 2019/06/29 00:11:28 [error] 6#6: *3 open() "/www/myapp-root/static/css/style.css" failed (2: No such file or directory),
谢谢你,