python - 在 docker 上运行 django+nginx+celery+gunicorn+daphne
问题描述
我一直在尝试使用 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?
这是目录结构
入口点.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 "$@"
你能指导我我做错了什么吗?
解决方案
推荐阅读
- sql - SQL 交互式文件路径
- excel - 如何根据多重标准和使用 OR 逻辑对值进行计数
- python - 为什么在构建 dict() 时会出现 TypeError?
- node.js - Dialogflow - 多个 agent.add 响应(nodejs)
- r - 为什么我的 anova 不能在闪亮的应用程序中工作
- qt-creator - QtCreator 在启动“文件错误”时崩溃
- amcharts - amcharts - 在图表外使用我自己的按钮导出图表
- python - 如何将数据添加到空 pd.dataframe python
- android - 单元测试,使用 PowerMock 在 Kotlin 中使用 Context 作为变量来模拟静态方法
- node.js - nodejs并发下如何正确使用redis的GET SET命令?