【发布时间】:2021-04-22 23:16:30
【问题描述】:
我在http://ruddra.com/2016/08/14/docker-django-nginx-postgres/ 一直关注 Django 和 docker 使用 nginx 的教程。但我有另一个暴露端口为 8000 的 VM,所以我将其更改为 8100,但将另一个保留为 8000
这是我的撰写文件:
version: '3'
services:
nginx:
image: nginx:latest
container_name: ngnix01
ports:
- "8100:8000"
volumes:
- ./code:/code
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: django01
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn mydjango.wsgi -b 0.0.0.0:8000"
depends_on:
- db
volumes:
- ./code:/code
expose:
- "8000"
db:
image: postgres:latest
container_name: postgres01
nginx 配置:
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
当我 docker-compose up 时出现错误
attaching to postgres01, django01, ngnix01
django01 | Traceback (most recent call last):
django01 | File "manage.py", line 8, in <module>
ngnix01 | 2017/08/25 10:25:01 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/django.conf:3
django01 | from django.core.management import execute_from_command_line
ngnix01 | nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/django.conf:3
django01 | ModuleNotFoundError: No module named 'django.core'
django01 exited with code 1
这让我觉得 nginx 无法在端口 8000 上联系独角兽?我认为我的撰写文件是正确的?左边的端口是要暴露的端口,右边的端口是容器中的端口?
或者是当使用多个图像时,它们会在外部相互交流?
谢谢
编辑: 包含dockerfile
#set base image
FROM python:latest
ENV PYTHONUNBUFFERED 1
#install dependencies
RUN sed -i "s#jessie main#jessie main contrib non-free#g" /etc/apt/sources.list
RUN apt-get update -y \
&& apt-get install -y apt-utils python-software-properties libsasl2-dev python3-dev libldap2-dev libssl-dev libsnmp-dev snmp-mibs-downloader
ENV APP_USER itapp
ENV APP_ROOT /code
RUN mkdir /code;
RUN groupadd -r ${APP_USER} \
&& useradd -r -m \
--home-dir ${APP_ROOT} \
-s /usr/sbin/nologin \
-g ${APP_USER} ${APP_USER}
WORKDIR ${APP_ROOT}
RUN mkdir /config
ADD config/requirements.txt /config/
RUN pip install -r /config/requirements.txt
USER ${APP_USER}
ADD . ${APP_ROOT}
【问题讨论】:
-
是的,您的文件似乎正确。如果您将主机地址指定为服务名称,则堆栈中的其他服务将尝试通过服务的公开(即内部 - 8000)端口进行连接。我想,django 容器中的问题导致 Nginx 丢失主机,因为 django 被快速杀死。 Docker 有时让我毛骨悚然……
-
您的应用是否有任何独立测试?您可以在 Dockerfile 中执行它们,然后在实际部署之前查看创建映像时是否出现任何问题。像这样的东西:
RUN python manage.py test --settings=app.settings.test -v2
标签: python django docker nginx