在docker上运行Django+nginx+芹菜+gunicorn+daphne

2024-04-27 05:10:19 发布

您现在位置:Python中文网/ 问答频道 /正文

长期以来,我一直试图使用docker运行django应用程序,但无法做到这一点。应用程序使用正常的架构(如部署在ubunutu计算引擎上)运行良好

下面是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"]

下面是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:

我得到以下错误

达芙妮

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

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

芹菜

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_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?

芹菜2

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?

下面是目录结构

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 "$@"

你能告诉我我做错了什么吗?


Tags: runenvyouwebapphomedbon