Horai Nuri
Horai Nuri

Reputation: 5568

How to setup nginx and django with docker-compose?

I'm having a situation while setting up Django and all the dependencies I need with Docker (docker-toolbox, docker-compose).

I'm encountering an error while I'm trying to access to my url http://192.168.99.100:8000/ which says 502 Bad Gateway (nginx/1.13.1). For this error I don't really understand from where it comes since it's the first time I'm using Django with nginx on Docker.

docker-compose.yml :

version: '2'  
services:  
  nginx:
    image: nginx:latest
    container_name: nz01
    ports:
      - "8000:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
      - /static:/static
    depends_on:
      - web
  web:
    ...
...

Dockerfile :

FROM python:latest
ENV PYTHONUNBUFFERED 1

#ENV C_FORCE_ROOT true

ENV APP_USER myapp
ENV APP_ROOT /src
RUN mkdir /src;
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.pip /config/
RUN pip install -r /config/requirements.pip

USER ${APP_USER}
ADD . ${APP_ROOT}

config/nginx/... .conf

upstream web {  
  ip_hash;
  server web:8000;
}

server {

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

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

Is there something I'm doing wrong ?

Upvotes: 2

Views: 3789

Answers (1)

Qasim Sarfraz
Qasim Sarfraz

Reputation: 6432

The issue here is with gunicorn command line argument, missing -b flag. It needs to be launched with -b flag for binding with the address specified. In your case gunicorn binds itself to default 127.0.0.1:8000 which isn't accessible to other containers. So, just change the command for web to following:

command: bash -c 'python manage.py makemigrations && python manage.py migrate && gunicorn oqtor.wsgi -b 0.0.0.0:8000'

That should make your web app accessible via nginx.

Upvotes: 3

Related Questions