rabbitmq 操作错误:[Errno 111]连接被拒绝(flask,docker-compose)

niwlg2el  于 11个月前  发布在  RabbitMQ
关注(0)|答案(1)|浏览(191)

我的flask app有问题
具有以下前提:
Python、Flask、Celerey、MongoDB、RabbitMQ。
当我向API发出请求时,我会得到以下返回:
错误:[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


网址: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)


网址: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


有谁能告诉我怎么解决这个问题吗?谢谢!

htzpubme

htzpubme1#

celery可能试图在代理可用之前连接到代理。
您可能需要向rabbitmq服务添加healthcheck,以便docker知道容器是否“就绪”。
否则,docker会假设rabbitmq服务在容器创建并启动CMD进程时就已经准备好了(即使该进程还没有沿着绑定到端口并开始监听连接)。
例如

version: '3.9'
services:
  ...

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

  ...

字符串
我可能最初是从https://devops.stackexchange.com/questions/12092/docker-compose-healthcheck-for-rabbitmq那里学到这个的

相关问题