3

I am trying to deploy my Django project in docker. unfortunately, I get 502 Bad Gateway error, I tried everything that I could find online, but still the same error.

The Dockerfile:


FROM python:3.9-slim-buster

# create directory for the app user
ENV APP_HOME=/home/app/web


# create the app user
RUN addgroup --system app && adduser --system --group app

# create the appropriate directories
RUN mkdir -p $APP_HOME

# where the code lives
WORKDIR $APP_HOME

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1


# install  python dependencies
RUN apt-get update \
    && apt-get install -y build-essential python3-dev python2.7-dev \
    libldap2-dev libsasl2-dev libssl-dev ldap-utils tox \
    lcov valgrind \
    && apt-get -y install gcc \
    && apt install -y netcat

# install psycopg2 dependencies
RUN apt-get install -y postgresql-server-dev-all musl-dev



# copy project
COPY . $APP_HOME

# install dependencies
COPY ./requirements.txt $APP_HOME
RUN pip install --upgrade pip
RUN pip install -r $APP_HOME/requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh $APP_HOME
RUN sed -i "s/\r$//g"  $APP_HOME/entrypoint.sh
RUN chmod +x  $APP_HOME/entrypoint.sh




# chown all the files to the app user
RUN chown -R app:app $APP_HOME 
RUN chmod -R 755 $APP_HOME 

# change to the app
USER app

# run entrypoint.sh
ENTRYPOINT ["/bin/bash","/home/app/web/entrypoint.sh"]

and the docker-compose.yml

version: '3.9'


services:
  nginx:
    build: ./nginx
    links:
     - "web"
    ports:
      - "80:80"
    depends_on:
      - web
    restart: "on-failure"
  web:
    build: 
      context: .
    command: bash -c 'gunicorn --bind 0.0.0.0:8000 culture_crawler.wsgi'
    # command: bash -c " gunicorn culture_crawler.wsgi:application --bind 0.0.0.0:8000"
    volumes:
      - .:/culture_crawler:rw
    expose:
      - 8000
    env_file:
      - ./.env
    depends_on:
      - db
    restart: "on-failure"
  db:
    image: postgres:13-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.db
    restart: "on-failure"

volumes:
  postgres_data:

the nginx.conf


upstream django {
    server web:8000;
}

server {

    listen 80;
    server_name  localhost;

    location / {
        proxy_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_redirect off;
        proxy_pass http://django;
        
    }


    
}

the entrypoint.sh

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "postgres database has initialized successfully"
fi

# python manage.py flush --no-input
# python manage.py migrate

exec "$@"

.env file

DEBUG=0
SECRET_KEY=change_me
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=culture_crawler_prod
SQL_USER=culture_crawler
SQL_PASSWORD=culture_crawler
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres

and finally the nginx Dockerfile

FROM nginx:1.21-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

I don't know where I am doing wrong. please help

Davood
  • 353
  • 4
  • 14
  • Does this answer your question? [Nginx reverse proxying to Django receiving \`upstream prematurely closed connection while reading response header from upstream\`](https://stackoverflow.com/questions/59002581/nginx-reverse-proxying-to-django-receiving-upstream-prematurely-closed-connecti) – P. Naoum Feb 15 '22 at 10:20
  • Make sure the Django application is running. Try setting the environment without nginx by running ***python manage.py runserver*** and confirm that the app is running as expected. – Nijo Feb 15 '22 at 10:27
  • @P.Naoum, unfortunately no – Davood Feb 15 '22 at 10:52
  • @Nijo yeah, it is working when I comment out the nginx service but it tasks a long time. could it be the reason? – Davood Feb 15 '22 at 10:54
  • @DavoodHadiannejad if tasks taking long time to complete without nginx, the nginx uwsgi connection might be getting closed as it does not get any response – Nijo Feb 15 '22 at 10:59
  • @Nijo yes it takes about 5 minutes to run the Django server. now what would be the solution? but it works just when I run the app with command: python manage.py runserver 0.0.0.0:8000 but not working with command: gunicorn culture_crawler.wsgi:application --bind 0.0.0.0:8000 – Davood Feb 15 '22 at 11:28
  • What is in the `entrypoint.sh` script? (It will receive the Compose `command:` as arguments; what does it do with them?) – David Maze Feb 15 '22 at 11:34
  • @DavoodHadiannejad 5 minutes for running with runserver command? – Nijo Feb 15 '22 at 11:39
  • @Nijo yes. that could be because of I am using so many ML libraries – Davood Feb 15 '22 at 11:44
  • @DavoodHadiannejad if the response time is ok, then try increasing proxy_read_timeout in nginx timeout. The response time has to be optimized from the application side. – Nijo Feb 15 '22 at 19:12

0 Answers0