Celery "consumer: Cannot connect to amqp://..." broker in CI
The Celery worker (or a task call) could not reach the AMQP broker named in broker_url. In CI this is a RabbitMQ service that is not up yet, or a broker URL that does not match the service host and port.
What this error means
Celery logs "consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused. Trying again in 2.00 seconds..." and the worker never becomes ready.
[ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//:
[Errno 111] Connection refused.
Trying again in 2.00 seconds... (1/100)Common causes
RabbitMQ service is not ready
The worker started before the broker container finished initializing, so the connection is refused and retried.
The broker URL does not match the service
broker_url points at a host/port the runner cannot reach (wrong service name, unpublished port).
How to fix it
Add a RabbitMQ service and matching broker URL
- Run a RabbitMQ service with a readiness check.
- Set
CELERY_BROKER_URLto the service host and port. - Start the worker only after the broker is reachable.
services:
rabbitmq:
image: rabbitmq:3
ports: ['5672:5672']
options: --health-cmd "rabbitmq-diagnostics -q ping" --health-interval 5s --health-retries 10
env:
CELERY_BROKER_URL: amqp://guest:guest@127.0.0.1:5672//Run tasks eagerly in tests
For unit tests that do not need a real broker, run tasks synchronously so no connection is required.
app.conf.task_always_eager = True
app.conf.task_eager_propagates = TrueHow to prevent it
- Gate the worker on a broker health check.
- Drive broker_url from env so CI matches the service.
- Use task_always_eager for tests that do not need a broker.