Rails "Redis::CannotConnectError: Connection refused" in CI
Rails tried to reach Redis for the cache, Action Cable, or Sidekiq and the connection was refused. In CI this means no Redis service container is running, or REDIS_URL points at a host and port nothing is listening on.
What this error means
A step fails with "Redis::CannotConnectError: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)".
Rails
Redis::CannotConnectError:
Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)Common causes
No Redis service container
The job uses a Redis-backed cache or queue but has no services: redis, so nothing listens on 6379.
REDIS_URL does not match the service
The URL targets the wrong host or port, or the service maps a different port, so the connection is refused.
How to fix it
Add a Redis service with a health check
- Declare a
redisservice and map port 6379. - Add a
redis-cli pinghealth check so steps wait for readiness. - Set REDIS_URL to the mapped host and port.
.github/workflows/ci.yml
services:
redis:
image: redis:7
ports: ['6379:6379']
options: >-
--health-cmd "redis-cli ping" --health-interval 10s
--health-timeout 5s --health-retries 5Point Rails at the Redis service
Export REDIS_URL so the cache, cable, and Sidekiq connect to the container.
.github/workflows/ci.yml
env:
REDIS_URL: redis://localhost:6379/0How to prevent it
- Add a Redis service and health check when tests use a Redis-backed store.
- Keep REDIS_URL aligned with the service port mapping.
- Use :test cache/queue adapters where a real Redis is not needed.
Related guides
Rails ActiveJob adapter not configured for test in CIFix Rails ActiveJob failures in CI where jobs hit a real backend - set queue_adapter to :test or :inline so t…
Rails "PG::ConnectionBad: could not connect to server" in CIFix Rails "PG::ConnectionBad: could not connect to server" in CI - the Postgres service container is not reac…
Rails parallel tests database connection error in CIFix Rails parallel test database connection errors in CI - parallelize spins up worker databases (myapp_test-…