简体   繁体   中英

docker compose Django nginx

I've been following a tutorial for Django and docker using nginx here http://ruddra.com/2016/08/14/docker-django-nginx-postgres/ . but I have another VM with an exposed port of 8000 so I changed it to 8100 but left the other at 8000

this is my compose file:

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 config:

upstream web {  
  ip_hash;
  server web:8000;
}

server {

    location /static/ {    
        autoindex on;    
        alias /static/; 
    }

    location / {
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}

and I'm getting the error when I 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

which makes me think nginx can't contact unicorn on port 8000? I think my compose file is correct? port on the left is port to expose, port on the right is the one in the container?

or is it that when using multiple images they speak to each other externally?

Thanks

EDIT: included 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}

the error is coming because there is no module django installed in the image you have to add the command pip install -r requirements.txt or add command pip install Django in command: bash section of image web

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:

Maybe you need to connect your three containers to the same network.

Try editing your docker-compose.yml like this:

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM