docker工作者组成Django nginx
我一直在使用nginx在这里http://ruddra.com/2016/08/14/docker-django-nginx-postgres/下面的Django和docker的教程。 但我有另一个8000的暴露端口的虚拟机,所以我把它改为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
nginxconfiguration:
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构build时遇到了这个错误
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端口上的独angular兽? 我认为我的撰写文件是正确的? 左边的端口是端口暴露的,右边的端口是容器中的端口?
或者当使用多个图像时,他们彼此对话?
谢谢
编辑:包括docker文件
#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}
错误是因为没有模块Django安装在图像中,您必须添加命令pip安装-r requirements.txt或添加命令pip安装Django在命令:图像网站的bash部分
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 networks: - default web: build: . container_name: django01 command: bash -c "pip install Django && cd /home/docker/code 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" networks: - default db: image: postgres:latest container_name: postgres01 networks: - default networks: default:
也许你需要将你的三个容器连接到同一个networking。
试试像这样编辑你的docker-compose.yml:
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 networks: - default 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" networks: - default db: image: postgres:latest container_name: postgres01 networks: - default networks: default: