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