debugdockerredisMajorpending
Debug: Redis connection refused in Docker
Viewed 0 times
redisdockerconnectionlocalhostnetworkservice-name
Error Messages
Problem
Application cannot connect to Redis when both run in Docker containers. Connection refused or timeout errors.
Solution
Docker networking issues between containers:
# 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
Bad: REDIS_URL=redis://localhost:6379 # Only works on host
Good: REDIS_URL=redis://redis:6379 # Service name = hostname
docker network create shared
docker run --network shared --name redis redis:7
docker run --network shared --name app myapp
# 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
# If binding to external interface:
redis:
command: redis-server --protected-mode no
- 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
- Common mistake - using localhost:
Bad: REDIS_URL=redis://localhost:6379 # Only works on host
Good: REDIS_URL=redis://redis:6379 # Service name = hostname
- 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
- 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
- Redis protected mode:
# If binding to external interface:
redis:
command: redis-server --protected-mode no
Revisions (0)
No revisions yet.