我正在嘗試在同一個 docker 容器中運行帶有 celery 和 gunicorn 的燒瓶應用程式。我為此負責。Gunicorn 的執行docker-compose.yml方式為
services:
web:
container_name: "flask"
build: ./
volumes:
- ./app:/app
ports:
- "8000:8000"
environment:
- DEPLOYMENT_TYPE=production
- FLASK_APP=app/main.py
- FLASK_DEBUG=1
- MONGODB_DATABASE=testdb
- MONGODB_USERNAME=testuser
- MONGODB_PASSWORD=testuser
- MONGODB_HOSTNAME=mongo
command: gunicorn app.main:app --workers 1 --name main --reload -b 0.0.0.0:8000 --preload
depends_on:
- redis
- mongo
links:
- mongo
在 supervisord.conf 中,設定了 celery
[supervisord]
nodaemon=true
[program:celeryworker]
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
command=/opt/venv/bin/celery -A app.routes.celery_tasks.celery worker --loglevel=info -B -s app/celerybeat-schedule
主管從 dockerfile 執行
FROM ubuntu:20.04
LABEL maintainer="nebu"
ENV GROUP_ID=1000 \
USER_ID=1000
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y apt-transport-https ca-certificates supervisor procps cron vim python3.8-venv python3-gdbm
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN ["python", "-m", "pip", "install", "--upgrade", "pip", "wheel"]
RUN apt-get install -y python3-wheel
COPY ./requirements.txt /app/requirements.txt
RUN ["python", "-m", "pip", "install", "--no-cache-dir", "--upgrade", "-r", "/app/requirements.txt"]
COPY ./app /app
RUN ["mkdir", "-p","/var/log/supervisor"]
COPY ./app/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]
但是當我把所有的容器都拿出來時,芹菜和節拍都沒有出現。所以我進入flask app容器的shell并執行/usr/bin/supervisord,然后celery worker就被啟動了,沒有任何錯誤。如何將 gunicorn 和 celery worker-beat 從同一個 docker 容器中結合在一起。
更新
當我使用下面的 supervisord.conf 時,nginx 沒有出現。它的顯示錯誤
[supervisord]
nodaemon=true
[program:celeryworker]
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
command=/opt/venv/bin/celery -A app.routes.celery_tasks.celery worker --loglevel=info -B -s app/celerybeat-schedule
[program:myproject_gunicorn]
user=root
command=gunicorn app.main:app --workers 1 --name main --reload -b 0.0.0.0:8000 --preload
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
錯誤
nginx exited with code 1
nginx | 2022/04/09 09:41:56 [emerg] 1#1: host not found in upstream "web" in /etc/nginx/conf.d/app.conf:8
nginx | nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/app.conf:8
nginx exited with code 1
nginx | 2022/04/09 09:41:59 [emerg] 1#1: host not found in upstream "web" in /etc/nginx/conf.d/app.conf:8
nginx | nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/app.conf:8
uj5u.com熱心網友回復:
CMD ["/usr/bin/supervisord"]在 dockerfile 和 docker command: gunicorn app.main:app --workers 1 --name main --reload -b 0.0.0.0:8000 --preload-compose.yml 中有沖突。
實際上,command在 docker-compose.yml 中會覆寫CMDdockerfile。這就是為什么只有gunicorn起來了。
我的建議:將 gunicorn 相關的東西添加到 supervisord.conf 并command在 docker-compose.yml 中洗掉。
這樣,supervisor 會把 gunicorn 和 celery 都帶上來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457925.html
下一篇:在非特權容器錯誤中創建符號鏈接
