3
votes

I'm developing a Django app with a PostgreSQL database and I'm using NGINX + Gunicorn with Docker.

PostgreSQL, NGINX and Gunicorn are on different containers communicating with networks. I can build my app with docker-compose build but when I execute it with docker-compose up and view my app in the browser all I get is a 502 Bad Gateway error and in the terminal all I see is this:

nginx_1   | 127.0.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.0" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "172.23.0.1"
nginx_1   | 172.23.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.1" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"

My docker-compose looks like this:

version: '3'

services:
  # Database container
  db:
    image: postgres:10
    volumes:
      - db_volume:/var/lib/postgresql/data
    env_file:
      - ./.env
    networks:
      - db_network

  # Web app container with gunicorn
  webapp:
    build: .
    env_file: ./.env

    volumes:
      - .:/opt/services/webapp/src
      - static:/opt/services/webapp/static
      - media:/opt/services/webapp/media
    networks:
      - db_network
      - nginx_network
    depends_on:
      - db

  # NGINX (Reverse proxy) container
  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static:/opt/services/webapp/static
      - media:/opt/services/webapp/media
    networks:
      - nginx_network
    depends_on:
      - webapp

networks:
  db_network:
    driver: bridge
  nginx_network:
    driver: bridge

volumes:
  db_volume:
  static:
  media:

And this is my Dockerfile:

# Start with an official Python image
FROM python:3.6

ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/webapp/src
WORKDIR /opt/services/webapp/src

# Install dependencies
ADD requirements.txt /opt/services/webapp/src
RUN pip install -r requirements.txt

COPY . /opt/services/webapp/src

# Expose port 8000
EXPOSE 8000

# Default command to run when starting the container
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "myapp", "myapp.wsgi:application"]

This is my requirements.txt:

bcrypt==3.1.4
cffi==1.11.5
Django==2.0.4
Pillow==5.1.0
psycopg2==2.7.4
psycopg2-binary==2.7.4
pycparser==2.18
pytz==2018.4
six==1.11.0
django-phonenumber-field==2.0.0
gunicorn==19.8.1
gevent==1.3.1

And my NGINX configuration:

# Upstream server
upstream myapp_server {
    server webapp:8000;
}

# Main server
server {
    listen 80;
    server_name localhost;

    location /static/ {
        alias /opt/services/webapp/static/;
    }

    location /media/ {
        alias /opt/services/webapp/media/;
    }

    location / {
        proxy_pass http://myapp_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1;
            break;
        }
    }
}

I'm not sure about what could be causing this problem but it looks like gunicorn is not properly detecting my app, NGINX is working and PostgreSQL seems to be working too!

1
I am having a similar problem have you got the solution for it?Avin Mathew

1 Answers

0
votes

You need use proxy_pass to your upstream.

    if (!-f $request_filename) {
        proxy_pass http://myapp_server;
        break;
    }