kombu.exceptions.OperationalError:[Errno 111]连接被拒绝(flask,docker-compose)

问题描述 投票:0回答:1

我的烧瓶应用程序遇到问题

拥有场地:

Python、Flask、Celerey、MongoDB、RabbitMQ。

当我向 API 发出请求时,我得到以下返回:

kombu.exceptions.OperationalError:[Errno 111]连接被拒绝

只有当我运行 docker-compose up 时才会发生这种情况,如果我尝试从 docker 外部的 Flask 启动应用程序,一切都会正常工作。

Dockerfile:

FROM python:3.11-alpine AS builder

RUN pip install poetry

WORKDIR /backend

COPY pyproject.toml poetry.lock ./

RUN poetry config virtualenvs.create false && poetry install --no-root

FROM python:3.11-alpine

WORKDIR /backend

COPY --from=builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/

COPY . .

docker-compose.yml

version: '3.9'
services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    restart: always
    ports:
      - 27017:27017
    volumes:
      - mongodb_data:/data/db

  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    restart: always
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=admin
      - RABBITMQ_DEFAULT_VHOST=/
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq

  celery_worker:
    build: .
    container_name: celery_worker
    environment:
      - CELERY_BROKER_URL=amqp://admin:admin@rabbitmq:5672/
    restart: always
    command: celery --app src.task worker --loglevel=info 
    depends_on:
      - mongodb
      - rabbitmq
      
  flask_app:
    build: .
    container_name: flask_app
    command: python src/app.py
    restart: always
    environment:
      - SERVER_HOST=0.0.0.0
      - SERVER_PORT=8080
    ports:
      - 5000:8080
    depends_on:
      - mongodb
      - rabbitmq
      - celery_worker
volumes:
  mongodb_data:  # Volume para persistência dos dados do MongoDB
  rabbitmq_data: # Volume para persistência dos dados do RabbitMQ

src/app.py

import os
from flask import Flask
from task import add

app = Flask(__name__)


@app.route('/', methods=['GET'])
def hello():
    add.delay(1, 2)
    return "Docker compose working!"


if __name__ == '__main__':
    host = os.environ.get('SERVER_HOST', 'localhost')
    port = os.environ.get('SERVER_PORT', '8001')
    print(f"Server listenning {host}:{port} !!!")
    app.run(host=host, port=port, debug=True)

src/task.py

import os 
from celery import Celery

broker = os.environ.get('CELERY_BROKER_URL', 'amqp://guest@localhost//')
celery = Celery('tasks')

@celery.task(queue='default')
def add(x, y):
    print(f'Adding {x} + {y}')
    return x + y

谁能告诉我如何解决这个问题?谢谢!!

python flask docker-compose rabbitmq celery
1个回答
0
投票

celery 可能会在代理可用之前尝试连接到代理。

您可能需要将

healthcheck
添加到您的
rabbitmq
服务,以便 docker 知道容器是否“就绪”。

否则,一旦创建容器并且启动其

rabbitmq
进程,docker 将假定
CMD
服务已准备就绪(即使该进程还没有进行到实际绑定到端口并开始侦听连接的程度) ).

例如

version: '3.9'
services:
  ...

  rabbitmq:
    image: rabbitmq:3-management
    ...
    healthcheck:
      test: rabbitmq-diagnostics -q ping
      interval: 30s
      timeout: 30s
      retries: 3

  ...
© www.soinside.com 2019 - 2024. All rights reserved.