我正在Django 2,Docker Compose和Nginx中构建练习应用程序。旋转图像成功,但是,任何非Django / Python文件都不会加载。
例如,不会加载/static/bootstrap-3.2/dist/css/bootstrap.css(或/ static中的任何文件)。我在SO上还看到了其他一些相关问题,但是由于某些原因,我认为我的配置仍然缺少一些东西。有控制台日志和终端行,指示静态或其他目录资源的404错误。
感谢任何帮助,谢谢!
WorkDir树
|- gunicorn/
|- nginx\
|-- nginx.conf
|-- ...
|- django/
|-- __init__.py
|-- templates/
|-- static/
|-- manage.py
|-- settings.py
|-- forms.py
|-- views.py
|-- urls.py
|-- wsgi.py
|- docker-compose.yml
|- Dockerfile
|- requirements.txt
Dockerfile
FROM python
ENV PYTHONUNBUFFERED 1
ADD . /ljingo
WORKDIR /ljingo
RUN pip install -r requirements.txt
RUN python -m nltk.downloader punkt
RUN python -m nltk.downloader wordnet
RUN python -m nltk.downloader averaged_perceptron_tagger
requirements.txt
Django==2.0
django-crispy-forms
psycopg2
gunicorn
nltk==3.3
nginx / nginx.conf
server {
root /;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /logs/access.log;
error_log /logs/error.log;
location /media {
alias /django/media;
}
location /static {
alias /django/static;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django:8000;
}
}
docker-compose.yml
version: '2'
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
- .:/ljingo
- ./nginx:/etc/nginx/conf.d
- ./static:/static
depends_on:
- django
django:
build: .
image: ljingo
command: python3 manage.py collectstatic
command: python3 manage.py migrate
# command: python3 manage.py runserver 0.0.0.0:80
command: gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000
# depends_on:
# - db
volumes:
- .:/ljingo
ports:
- "8000:8000"
终端STDOUT输出
mac:django-gunicorn bmalone$ docker-compose up
django-gunicorn_django_1 is up-to-date
Starting django-gunicorn_nginx_1 ... done
Attaching to django-gunicorn_django_1, django-gunicorn_nginx_1
django_1 | [2018-08-01 00:23:22 +0000] [1] [INFO] Starting gunicorn 19.9.0
django_1 | [2018-08-01 00:23:22 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
django_1 | [2018-08-01 00:23:22 +0000] [1] [INFO] Using worker: sync
django_1 | [2018-08-01 00:23:22 +0000] [9] [INFO] Booting worker with pid: 9
django_1 | [2018-08-01 00:23:23 +0000] [10] [INFO] Booting worker with pid: 10
django_1 | [2018-08-01 00:23:23 +0000] [11] [INFO] Booting worker with pid: 11
django_1 | [2018-08-01 00:23:23 +0000] [12] [INFO] Booting worker with pid: 12
django_1 | [2018-08-01 00:23:23 +0000] [13] [INFO] Booting worker with pid: 13
django_1 | Not Found: /static/custom.css
django_1 | Not Found: /static/bootstrap-3.2/dist/css/bootstrap.css
django-gunicorn_nginx_1 exited with code 0
答案 0 :(得分:1)
您的docker-compose.yml
文件不能包含多个command
条目。您需要将对manage.py
和gunicorn
的调用合并到一个命令中。
command: python3 manage.py collectstatic && python3 manage.py migrate && gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000
由于这有点冗长且难以阅读,因此我建议创建一个简单的bash脚本来运行每个命令,并从docker-compose.yml
配置中调用该脚本:
start_django.sh
#!/usr/bin/env bash
set -ex
python3 manage.py collectstatic
python3 manage.py migrate
gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000
docker-compose.yml
django:
# ...all of the other config goes here, but a single command entry...
command: ./start_django.sh
确保创建了start_django.sh
后,便在容器内为其赋予了可执行权限。可以使用您的Dockerfile
:
Dockerfile
ADD . /ljingo
RUN chmod +x /lingo/start_django.sh
WORKDIR /ljingo