docker编写Django nginx

2024-10-03 04:25:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用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

nginx配置:

^{pr2}$

我在docker合成时得到了错误

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}

Tags: django端口runindevwebconfigapp
2条回答

由于映像中没有安装模块django,因此出现错误 您必须添加命令pip install-r要求.txt或者在imageweb的command:bash部分添加命令pip install Django

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:

也许你需要把你的三个容器连接到同一个网络。在

尝试编辑你的docker-合成.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:

相关问题 更多 >