Use this pattern for local development, staging, or controlled lab environments.
sudo mkdir -p /opt/redis
cd /opt/redis
compose.yamlservices:
redis:
image: redis:8.6
restart: unless-stopped
ports:
- "6379:6379"
volumes:
- ./data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-net
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
networks:
redis-net:
driver: bridge
Create redis.conf in your project directory:
# Network
bind 0.0.0.0
port 6379
protected-mode yes
# Authentication
requirepass ${REDIS_PASSWORD}
# Memory and eviction
maxmemory 512mb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# Logging
loglevel notice
# Data dir
dir /data
# Security
rename-command FLUSHALL ""
rename-command CONFIG ""
Create a .env file:
REDIS_PASSWORD=your_secure_password_here
docker compose up -d
docker compose ps
For production environments, consider using TLS:
services:
redis:
image: redis:8.6
restart: unless-stopped
ports:
- "6380:6380"
volumes:
- ./data:/data
- ./certs:/certs:ro
- ./redis-tls.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-net
healthcheck:
test: ["CMD", "redis-cli", "--tls", "--cert", "/certs/client.crt", "--key", "/certs/client.key", "--cacert", "/certs/ca.crt", "ping"]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
networks:
redis-net:
driver: bridge
With corresponding redis-tls.conf:
# TLS Configuration
tls-port 6380
port 0
tls-cert-file /certs/redis.crt
tls-key-file /certs/redis.key
tls-ca-cert-file /certs/ca.crt
tls-auth-clients yes
# Authentication
requirepass ${REDIS_PASSWORD}
# Memory and eviction
maxmemory 512mb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# Logging
loglevel notice
# Data dir
dir /data
docker compose logs redis
docker exec -it redis-redis-1 redis-cli -a 'your_password'
docker exec redis-redis-1 redis-cli -a 'your_password' BGSAVE
# Then copy the dump.rdb file from the volume
docker cp redis-redis-1:/data/dump.rdb ./backup/
redis:8.6 instead of redis:latest).