我一直在關注Django和docker的教程,在這裏使用nginx http://ruddra.com/2016/08/14/docker-django-nginx-postgres/。但我有另一個VM與8000暴露的端口,所以我把它改成8100,但留下另一個在8000docker撰寫Django nginx
這是我撰寫的文件:
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;
}
我得到錯誤當我碼頭 - 組成了
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快速死亡。 碼頭工人有時候會讓我有些毛骨悚然...... –
你有沒有爲你的應用程序進行任何獨立的測試? 您可以在Dockerfile中執行它們,然後在實際部署之前,您會看到是否在創建映像時發生任何錯誤。 類似這樣:'運行python manage.py test --settings = app.settings.test -v2' –