抱歉,如果答案看起来很明显,但过去几个小时我一直在摇头。
我一直在关注多个教程,尝试对我的应用程序进行 docker 化。无论我尝试使用 url:port 或只是 url 的组合,我都无法访问这些页面。
我对我做错了什么一无所知。我假设如下:
- NGINX 配置完全错误。
上游网络
如何知道web
是什么?我假设 docker 公开了它,但不确定这是否正确。 - 网络容器未正确公开地址和端口?
我尝试了多种设置,但不起作用。
我有以下内容:
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
ADD ./django.conf /etc/nginx/conf.d/default.conf
#COPY ./django.conf /etc/nginx/sites-available/
#RUN ln -s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled
docker-compose.yml
version: '3'
services:
db:
image: postgres:9.6
ports:
- "5432:5432"
environment:
- POSTGRES_USER=random_user
- POSTGRES_PASSWORD=random_password
redis:
restart: always
image: redis:4.0
expose:
- "6379"
nginx:
image: nginx:1.14.0
container_name: nginx01
ports:
- "8000:8000"
volumes:
- ./code
depends_on:
- web
web:
build: .
container_name: backend-api
command: bash -c "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn ops4_backend.wsgi -b 0.0.0.0:8000"
depends_on:
- db
volumes:
- ./code
expose:
- "8000"
restart: always
django.conf
upstream web {
ip_hash;
server web:8000;
}
# portal
server {
location / {
proxy_pass http://web:8000;
}
listen 8000;
server_name localhost;
location /static {
autoindex on;
alias /src/static/;
}
}
最佳答案
我昨天做了几乎相同的任务,我看到的唯一有值(value)的区别是你在 Django 容器中包含 django.conf ,而不是在 nginx 容器中。我在 docker-compose.yml
的 nginx
部分中有以下卷:
- ./nginx:/etc/nginx/conf.d
(其中 ./nginx
是包含 django.conf
的文件夹)
编辑:这是我的docker-compose.yml
:
version: '3'
services:
nginx:
image: nginx
restart: always
ports:
- "80:8000"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./static_cdn:/static
depends_on:
- web
db:
image: postgres
restart: always
web:
build: .
restart: always
command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --no-input && gunicorn core.wsgi -b 0.0.0.0:8000"
volumes:
- .:/code
expose:
- "8000"
depends_on:
- db
django.conf
:
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
alias /static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
通过此设置,nginx 可在 127.0.0.1:80
或 127.0.0.1
上使用,因为 80 是默认的 http 端口。
关于Django Nginx Docker Gunicorn 无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51557216/