docker
Docker Compose with Redis
Docker Compose config for Redis with persistence, authentication, memory limits, and Redis Commander UI.
Overview
A Docker Compose setup for Redis as a caching layer, session store, or message broker. Includes persistence options (RDB snapshots and AOF logging), password authentication, memory management, and an optional web UI.
Configuration
# docker-compose.yml
services:
redis:
image: redis:7-alpine # Latest Redis 7 on Alpine
container_name: app-redis
restart: unless-stopped
ports:
- "6379:6379" # Default Redis port
volumes:
- redis_data:/data # Persist data between restarts
# Redis configuration via command flags
command: >
redis-server
--requirepass ${REDIS_PASSWORD:-redispass}
--maxmemory 256mb
--maxmemory-policy allkeys-lru
--appendonly yes
--appendfsync everysec
--save 900 1
--save 300 10
--save 60 10000
--tcp-backlog 511
--timeout 300
--tcp-keepalive 60
--loglevel notice
healthcheck:
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redispass}", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
# Limit container resources
deploy:
resources:
limits:
memory: 512M # Hard memory ceiling
cpus: '0.5'
redis-commander:
image: rediscommander/redis-commander:latest
container_name: app-redis-commander
restart: unless-stopped
profiles:
- tools # Only starts with --profile tools
environment:
REDIS_HOSTS: "local:redis:6379:0:${REDIS_PASSWORD:-redispass}"
HTTP_USER: admin # Basic auth for web UI
HTTP_PASSWORD: ${REDIS_UI_PASSWORD:-admin}
ports:
- "8081:8081" # Web UI port
depends_on:
redis:
condition: service_healthy
volumes:
redis_data:
driver: local
Key Options Explained
--requirepass— Enables password authentication. Always set this, even in development, to build good habits.--maxmemory 256mb— Caps Redis memory usage. Without this, Redis will use all available memory and potentially get OOM-killed.--maxmemory-policy allkeys-lru— When memory limit is reached, evict the least recently used keys. Other options:volatile-lru(only keys with TTL),noeviction(return errors).--appendonly yes— Enables AOF (Append Only File) for durability. Every write is logged so data survives restarts.--appendfsync everysec— Flushes AOF to disk every second. Balances durability and performance (at most 1 second of data loss).--save 900 1— Creates an RDB snapshot if at least 1 key changed in 900 seconds. Multiplesaverules provide tiered snapshotting.--timeout 300— Closes idle client connections after 300 seconds to free up resources.
Common Modifications
- Disable persistence for pure cache: Remove
--appendonlyand--saveflags. Add--save ""to explicitly disable RDB snapshots. - Redis Cluster: Use multiple Redis instances with
redis:7-alpineand cluster configuration for horizontal scaling. - Custom config file: Mount with
- ./redis.conf:/usr/local/etc/redis/redis.confand change command toredis-server /usr/local/etc/redis/redis.conf. - Sentinel for HA: Add Redis Sentinel containers to provide automatic failover in production-like environments.
- Key expiration defaults: Add
--default-ttl 3600or set TTLs in your application code to prevent unbounded cache growth.