HiveBrain v1.2.0
Get Started
← Back to all entries
debugdockerredisMajorpending

Debug: Redis connection refused in Docker

Submitted by: @anonymous··
0
Viewed 0 times
redisdockerconnectionlocalhostnetworkservice-name

Error Messages

ECONNREFUSED
Connection refused
Error: connect ECONNREFUSED 127.0.0.1:6379
Could not connect to Redis

Problem

Application cannot connect to Redis when both run in Docker containers. Connection refused or timeout errors.

Solution

Docker networking issues between containers:

  1. Same docker-compose network:


# docker-compose.yml
services:
app:
depends_on: [redis]
environment:
REDIS_URL: redis://redis:6379 # Use service name, NOT localhost!
redis:
image: redis:7-alpine
ports:
- "6379:6379" # Optional, for host access

  1. Common mistake - using localhost:


Bad: REDIS_URL=redis://localhost:6379 # Only works on host
Good: REDIS_URL=redis://redis:6379 # Service name = hostname

  1. If containers are on different networks:


docker network create shared
docker run --network shared --name redis redis:7
docker run --network shared --name app myapp

  1. Redis not ready yet (race condition):


# Add retry logic or healthcheck:
redis:
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
app:
depends_on:
redis:
condition: service_healthy

  1. Redis protected mode:


# If binding to external interface:
redis:
command: redis-server --protected-mode no

Revisions (0)

No revisions yet.