我正在尝试在我的 docker flask 项目中使用 telnet 来使用 rdb 调试 celery 任务。
为此,我在我的 dockerfile 中安装了 telnet,然后为了测试它,我使用:
docker-compose exec web bash
,web 基本上是我的烧瓶服务器,然后运行 flask shell
进行测试。
然后我运行
from project.users.tasks import divide
,将我的测试任务划分为以下代码:
@shared_task
def divide(x, y):
rdb.set_trace()
import time
time.sleep(5)
return 10*x / y
似乎一切都很好,因为当我运行
divide.delay(1, 2)
时,我收到:
Remote Debugger:6899: Ready to connect: telnet 127.0.0.1 6899
Type `exit` in session to continue.
Remote Debugger:6899: Waiting for client...
但我的问题是,当我进入
docker-compose exec celery_worker bash
并尝试运行telnet 127.0.0.1 6899
时,我收到:
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
有人知道我现在做错了什么吗?因为我真的不明白这里的问题是什么......
提前致谢!
编辑:如果我回到 docker-compose exec web bash 并运行 telnet 而不是其他地方,则与 telnet 的连接有效。
顺便说一句,我的撰写文件如下:
version: '3.8'
services:
web:
build:
context: .
dockerfile: ./compose/local/flask/Dockerfile
image: flask_celery_example_web
# '/start' is the shell script used to run the service
command: /start
# this volume is used to map the files and folders on the host to the container
# so if we change code on the host, code in the docker container will also be changed
volumes:
- .:/app
ports:
- 5010:5000
env_file:
- .env/.dev-sample
environment:
- FLASK_APP=app
depends_on:
- redis
- db
db:
image: postgres:14-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=flask_celery
- POSTGRES_USER=flask_celery
- POSTGRES_PASSWORD=flask_celery
redis:
image: redis:7-alpine
celery_worker:
build:
context: .
dockerfile: ./compose/local/flask/Dockerfile
image: flask_celery_example_celery_worker
command: /start-celeryworker
volumes:
- .:/app
env_file:
- .env/.dev-sample
environment:
- FLASK_APP=app
depends_on:
- redis
- db
celery_beat:
build:
context: .
dockerfile: ./compose/local/flask/Dockerfile
image: flask_celery_example_celery_beat
command: /start-celerybeat
volumes:
- .:/app
env_file:
- .env/.dev-sample
environment:
- FLASK_APP=app
depends_on:
- redis
- db
flower:
build:
context: .
dockerfile: ./compose/local/flask/Dockerfile
image: flask_celery_example_celery_flower
command: /start-flower
volumes:
- .:/app
env_file:
- .env/.dev-sample
environment:
- FLASK_APP=app
ports:
- 5557:5555
depends_on:
- redis
- db
volumes:
postgres_data:
还有我的 Dockerfile :
FROM python:3.9-slim-buster
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
# psycopg2 dependencies
&& apt-get install -y libpq-dev \
# Additional dependencies
&& apt-get install -y telnet netcat \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
COPY ./compose/local/flask/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint
COPY ./compose/local/flask/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
COPY ./compose/local/flask/celery/worker/start /start-celeryworker
RUN sed -i 's/\r$//g' /start-celeryworker
RUN chmod +x /start-celeryworker
COPY ./compose/local/flask/celery/beat/start /start-celerybeat
RUN sed -i 's/\r$//g' /start-celerybeat
RUN chmod +x /start-celerybeat
COPY ./compose/local/flask/celery/flower/start /start-flower
RUN sed -i 's/\r$//g' /start-flower
RUN chmod +x /start-flower
WORKDIR /app
ENTRYPOINT ["/entrypoint"]