Trying to deploy a django server with gunicorn, nginx, and docker

  django, docker, nginx

I’m trying to follow this, but I’m finding some issues.

My folder structure is a follows:

— project
    — config/nginx/conf.d
    — www
        — server
            — settings.py, wsgi.py, etc.
        — Dockerfile
        — manage.py
        — requirements.txt
    — docker-compose.yml
    — Dockerfile

The most relevant files are as follows:

Dockerfile

FROM python:3

# Make the Python output not buffered; logs will show in real time
ENV PYTHONUNBUFFERED=1

WORKDIR /www

COPY requirements.txt /www/

RUN pip install -r requirements.txt

COPY . /www/

EXPOSE 80

CMD ["gunicorn", "--bind", ":80", "server.wsgi:application"]

docker-compose.yml

version: "3.9"
   
services:
  web:
    build: ./www
    volumes:
      - .:/web
  nginx:
    image: nginx:1.13
    ports:
      - 80:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - web
    networks:
      - nginx_network

networks:
  nginx_network:
    driver: bridge

local.conf


upstream www {
    server web:80;
}

server {

    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://web;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

This is the output I get:

web_1    | [2021-10-28 14:44:43 +0000] [1] [INFO] Starting gunicorn 20.1.0
web_1    | [2021-10-28 14:44:43 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
web_1    | [2021-10-28 14:44:43 +0000] [1] [INFO] Using worker: sync
web_1    | [2021-10-28 14:44:43 +0000] [7] [INFO] Booting worker with pid: 7
nginx_1  | 2021/10/28 14:44:46 [emerg] 1#1: host not found in upstream "web:80" in /etc/nginx/conf.d/local.conf:3
nginx_1  | nginx: [emerg] host not found in upstream "web:80" in /etc/nginx/conf.d/local.conf:3
project_nginx_1 exited with code 1

I’m thinking that I might be getting the server name wrong in local.conf, here:

upstream www {
    server web:80;
}

But I’ve tried www, web, server, and a I think a few more… What is wrong with this?

Source: Docker Questions

LEAVE A COMMENT