Waiting for Redis "Ready to accept connections" in CI before tests
redis-server prints "Ready to accept connections" only after it finishes loading, and connections before that line are refused. The reliable gate in CI is to poll redis-cli ping until it returns PONG, or to attach a health check to the service.
What this error means
The first test occasionally fails with "Connection refused" and passes on re-run. The Redis log shows the "Ready to accept connections" line landing a moment after the job started connecting.
1:M ... * Ready to accept connections tcp
# but the test ran a moment earlier and got:
Could not connect to Redis at localhost:6379: Connection refusedCommon causes
A race between service boot and the first command
The container is reported created before redis-server is listening, so an eager client wins the race and gets refused.
No readiness gate in the workflow
Without a health check or PING loop, the job proceeds the instant the container exists rather than when Redis is ready.
How to fix it
Poll redis-cli ping until PONG
Block the job until Redis answers, then continue. This is deterministic and fast.
until redis-cli -h localhost -p 6379 ping | grep -q PONG; do
echo "waiting for redis to be ready..."; sleep 1
done
echo "redis is ready"Attach a service health check
Let the runner hold the job until the service is healthy so tests never see a refused socket.
services:
redis:
image: redis:7
ports: ['6379:6379']
options: >-
--health-cmd "redis-cli ping"
--health-interval 5s
--health-retries 10How to prevent it
- Never connect on the assumption that a created container is ready.
- Use a service health check or a PING loop as a hard gate.
- Keep the readiness gate in a dedicated step before tests.