Run django+nginx+celery+gunicorn+daphne on docker

Viewed 644

I have been trying for long to run my django application using docker but unable to do so. Application works fine using normal architecture like deploying on ubunutu compute engine.

Below is the Dockerfile.prod

#########
# FINAL #
#########

# pull official base image
FROM ubuntu:20.04

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN adduser app
RUN usermod -a -G app app


# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
WORKDIR $APP_HOME

# install dependencies
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update 
RUN apt-get upgrade
RUN apt-get install python3-pip nano  -y
RUN apt-get install postgresql-contrib libpq-dev python-dev -y
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt


# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

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

# change to the app user
USER app

RUN ls

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

Below is docker-compose.prod.yml

version: '3.7'

services:
  web:
    build:
      context: ./app
      dockerfile:  Dockerfile.prod
    command: gunicorn Reporting.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
      - rabbitmq
      - redis

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db


  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1338:80
    depends_on:
      - web

  redis:
    image: redis:5.0.12-alpine


  rabbitmq:
    image: rabbitmq


  daphne:
    build:
      context: ./app
      dockerfile:  Dockerfile.prod
    command: daphne -b 0.0.0.0 -p 8001 Reporting.asgi:application
    expose:
      - 8001
    env_file:
      - ./.env.prod
    depends_on:
      - db   
      - redis
      - rabbitmq 


  celery:
    build:
      context: ./app
      dockerfile:  Dockerfile.prod
    command: celery -A Reporting worker -l info -n worker1@%h --concurrency=3
    volumes:
      - app_volume:/usr/src/app/
    env_file:
      - ./.env.prod.db
    depends_on:
      - redis


  celery2:
    build:
      context: ./app
      dockerfile:  Dockerfile.prod
    command: celery -A Reporting worker -l info -Q status  -n worker2@%h --concurrency=1
    volumes:
      - app_volume:/usr/src/app/
    env_file:
      - ./.env.prod.db
    depends_on:
      - redis
      - rabbitmq


  celery-beat:
    build:
      context: ./app
      dockerfile:  Dockerfile.prod
    command: celery -A Reporting beat --scheduler django_celery_beat.schedulers:DatabaseScheduler -l info
    volumes:
      - app_volume:/usr/src/app/
    env_file:
      - ./.env.prod.db
    depends_on:
      - db
      - redis
      - rabbitmq

volumes:
  app_volume:
  postgres_data:
  static_volume:

I am getting the below errors

daphne

daphne_1       | /home/app/web/entrypoint.prod.sh: 7: nc: not found

web

web_1          | /home/app/web/entrypoint.prod.sh: 7: nc: not found

celerybeat

celery-beat_1  | Traceback (most recent call last):
celery-beat_1  |   File "manage.py", line 17, in <module>
celery-beat_1  |     "Couldn't import Django. Are you sure it's installed and "
celery-beat_1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

celery

celery_1       | Traceback (most recent call last):
celery_1       |   File "manage.py", line 17, in <module>
celery_1       |     "Couldn't import Django. Are you sure it's installed and "
celery_1       | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

celery2

celery2_1      | Traceback (most recent call last):
celery2_1      |   File "manage.py", line 17, in <module>
celery2_1      |     "Couldn't import Django. Are you sure it's installed and "
celery2_1      | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Here is the directory structure

enter image description here

entrypoint.prod.sh

#!/bin/sh

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

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

    echo "PostgreSQL started"
fi

python manage.py collectstatic --no-input --clear

exec "$@"

Can you please guide me what am i doing wrong?

0 Answers
Related