Django 应用无法与 docker 和 nginx 一起使用
Django app isn't working with docker and nginx
我正在尝试启动 django + docker + nginx 网络应用程序
我在 docker-compose 日志中没有看到任何错误。完整日志:https://pastebin.com/tUpf5Wv0
但是本地 url 0.0.0.0:80 或 127.0.0.1:80 不工作。
问题似乎与 nginx 有关,但我在日志中没有看到任何错误。如何找到问题的原因?
应用程序结构(完整代码https://github.com/mascai/django_docker_nginx_template)
.
├── docker-compose.yml
├── project
│ ├── core_app
...
│ ├── nginx-conf.d
│ │ └── nginx-conf.conf
│ ├── parser_app
...
Docker文件
FROM python:3.9-alpine
WORKDIR /project
COPY . .
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev && \
pip install --no-cache-dir -r requirements.txt
nginx-conf.conf
upstream app {
server django:8000;
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://django:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /var/www/html/static/;
}
}
Docker 撰写文件(看起来不错)
version: '3.9'
services:
django:
build: ./project # path to Dockerfile
command: sh -c "gunicorn --bind 0.0.0.0:8000 core_app.wsgi:application"
volumes:
- ./project:/project
- static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=MY_PASSWORD
- POSTGRES_DB=lk_potok_4
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- static:/var/www/html/static
- ./nginx-conf.d/:/etc/nginx/conf.d
volumes:
pg_data:
static:
发现问题涉及 docker-compose 文件中的无效卷
修复:./project/nginx-conf.d/:/etc/nginx/conf.d
这个版本适合我:
version: '3.9'
services:
django:
build: ./project # path to Dockerfile
command: sh -c "
python manage.py makemigrations
&& python manage.py migrate
&& gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
volumes:
- ./project:/project
- ./project/static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=post222
- POSTGRES_DB=lk_potok_4
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- ./project/static:/var/www/html/static
- ./project/nginx-conf.d/:/etc/nginx/conf.d
celery:
build: ./project
command: celery -A core_app worker --loglevel=info
volumes:
- ./project:/usr/src/app
environment:
- DEBUG=1
- DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
- CELERY_BROKER=redis://redis:6379/0
- CELERY_BACKEND=redis://redis:6379/0
depends_on:
- django
- redis
redis:
image: redis:5-alpine
volumes:
pg_data:
static:
我正在尝试启动 django + docker + nginx 网络应用程序
我在 docker-compose 日志中没有看到任何错误。完整日志:https://pastebin.com/tUpf5Wv0 但是本地 url 0.0.0.0:80 或 127.0.0.1:80 不工作。
问题似乎与 nginx 有关,但我在日志中没有看到任何错误。如何找到问题的原因?
应用程序结构(完整代码https://github.com/mascai/django_docker_nginx_template)
.
├── docker-compose.yml
├── project
│ ├── core_app
...
│ ├── nginx-conf.d
│ │ └── nginx-conf.conf
│ ├── parser_app
...
Docker文件
FROM python:3.9-alpine
WORKDIR /project
COPY . .
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev && \
pip install --no-cache-dir -r requirements.txt
nginx-conf.conf
upstream app {
server django:8000;
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://django:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /var/www/html/static/;
}
}
Docker 撰写文件(看起来不错)
version: '3.9'
services:
django:
build: ./project # path to Dockerfile
command: sh -c "gunicorn --bind 0.0.0.0:8000 core_app.wsgi:application"
volumes:
- ./project:/project
- static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=MY_PASSWORD
- POSTGRES_DB=lk_potok_4
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- static:/var/www/html/static
- ./nginx-conf.d/:/etc/nginx/conf.d
volumes:
pg_data:
static:
发现问题涉及 docker-compose 文件中的无效卷
修复:./project/nginx-conf.d/:/etc/nginx/conf.d
这个版本适合我:
version: '3.9'
services:
django:
build: ./project # path to Dockerfile
command: sh -c "
python manage.py makemigrations
&& python manage.py migrate
&& gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
volumes:
- ./project:/project
- ./project/static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=post222
- POSTGRES_DB=lk_potok_4
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- ./project/static:/var/www/html/static
- ./project/nginx-conf.d/:/etc/nginx/conf.d
celery:
build: ./project
command: celery -A core_app worker --loglevel=info
volumes:
- ./project:/usr/src/app
environment:
- DEBUG=1
- DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
- CELERY_BROKER=redis://redis:6379/0
- CELERY_BACKEND=redis://redis:6379/0
depends_on:
- django
- redis
redis:
image: redis:5-alpine
volumes:
pg_data:
static: