Celery "Cannot connect to redis://..." broker/backend in CI
Celery could not reach the Redis broker or result backend in its URL. In CI this is a Redis service that has not finished starting, or a URL whose host/port does not match the service.
What this error means
Celery logs "consumer: Cannot connect to redis://127.0.0.1:6379//: Error 111 connecting to 127.0.0.1:6379. Connection refused." and retries without becoming ready.
python
[ERROR/MainProcess] consumer: Cannot connect to redis://127.0.0.1:6379//:
Error 111 connecting to 127.0.0.1:6379. Connection refused..
Trying again in 2.00 seconds... (1/100)Common causes
Redis service is not ready
The worker connected before the Redis container started accepting connections.
The Redis URL is wrong for CI
broker_url or result_backend points at a host/port the runner cannot reach.
How to fix it
Add a Redis service with a health check
- Run a Redis service that reports healthy via redis-cli ping.
- Set broker and backend URLs to the service host and port.
- Start the worker after Redis is healthy.
.github/workflows/ci.yml
services:
redis:
image: redis:7
ports: ['6379:6379']
options: --health-cmd "redis-cli ping" --health-interval 5s --health-retries 10
env:
CELERY_BROKER_URL: redis://127.0.0.1:6379/0
CELERY_RESULT_BACKEND: redis://127.0.0.1:6379/1Run tasks eagerly for unit tests
Tests that do not exercise the broker can run tasks synchronously and skip Redis entirely.
python
app.conf.task_always_eager = TrueHow to prevent it
- Gate the worker on a Redis health check.
- Drive broker/backend URLs from env.
- Use task_always_eager where a real broker is not needed.
Related guides
Celery "consumer: Cannot connect to amqp://..." broker in CIFix Celery "consumer: Cannot connect to amqp://guest@127.0.0.1:5672//: Connection refused" in CI - the worker…
Celery "Received unregistered task of type" in CIFix Celery "Received unregistered task of type 'X'. The message has been ignored and discarded" in CI - the w…
SQLAlchemy "OperationalError ... Connection refused" in CIFix SQLAlchemy "sqlalchemy.exc.OperationalError ... could not connect ... Connection refused" in CI - the eng…