Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Created February 15, 2025 07:47
Show Gist options
  • Save jdnichollsc/905302a29b3cc7ebd44eb10753381b01 to your computer and use it in GitHub Desktop.
Save jdnichollsc/905302a29b3cc7ebd44eb10753381b01 to your computer and use it in GitHub Desktop.
Enable Redis Streams, TimeSeries and Cluster mode with Docker
version: '3.6'
# Define name templates
x-name-templates:
project-name: &project-name ${COMPOSE_PROJECT_NAME:-projectx}
service-names:
redis-node-0: &name-redis-0 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-0
redis-node-1: &name-redis-1 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-1
redis-node-2: &name-redis-2 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-2
redis-node-3: &name-redis-3 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-3
redis-node-4: &name-redis-4 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-4
redis-node-5: &name-redis-5 ${COMPOSE_PROJECT_NAME:-projectx}-redis-node-5
redis-cluster: &name-redis-cluster ${COMPOSE_PROJECT_NAME:-projectx}-redis-cluster
redis-insight: &name-redis-insight ${COMPOSE_PROJECT_NAME:-projectx}-redis-insight
redis-commander: &name-redis-commander ${COMPOSE_PROJECT_NAME:-projectx}-redis-commander
volume-names:
redis-0: &vol-redis-0 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-0
redis-1: &vol-redis-1 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-1
redis-2: &vol-redis-2 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-2
redis-3: &vol-redis-3 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-3
redis-4: &vol-redis-4 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-4
redis-5: &vol-redis-5 ${COMPOSE_PROJECT_NAME:-projectx}-redis-data-5
network-names:
default: &network-default ${COMPOSE_PROJECT_NAME:-projectx}-network
# Redis node common configuration
x-redis-node: &redis-node
image: redis/redis-stack:7.2.0-v15
command: redis-server --port 6379
--cluster-enabled yes
--cluster-config-file nodes.conf
--cluster-node-timeout 5000
--appendonly yes
--save 60 1000
--bind 0.0.0.0
--requirepass ${REDIS_PASSWORD}
--masterauth ${REDIS_PASSWORD}
--loadmodule /opt/redis-stack/lib/redisearch.so
--loadmodule /opt/redis-stack/lib/redistimeseries.so
environment:
- REDIS_ARGS=--save 60 1000 --appendonly yes
- REDISEARCH_ARGS=MAXSEARCHRESULTS 10000
- REDISJSON_ARGS=MAXMEMORY 2gb
- REDISTIMESERIES_ARGS=RETENTION_POLICY=24h
healthcheck:
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
interval: 5s
timeout: 5s
retries: 20
start_period: 5s
services:
# Redis Cluster Configuration
redis-node-0:
<<: *redis-node
container_name: *name-redis-0
ports:
- "6379:6379"
- "8001:8001"
volumes:
- redis_data_0:/data
redis-node-1:
<<: *redis-node
container_name: *name-redis-1
ports:
- "6380:6379"
- "8002:8001"
volumes:
- redis_data_1:/data
redis-node-2:
<<: *redis-node
container_name: *name-redis-2
ports:
- "6381:6379"
- "8003:8001"
volumes:
- redis_data_2:/data
redis-node-3:
<<: *redis-node
container_name: *name-redis-3
ports:
- "6382:6379"
- "8004:8001"
volumes:
- redis_data_3:/data
redis-node-4:
<<: *redis-node
container_name: *name-redis-4
ports:
- "6383:6379"
- "8005:8001"
volumes:
- redis_data_4:/data
redis-node-5:
<<: *redis-node
container_name: *name-redis-5
ports:
- "6384:6379"
- "8006:8001"
volumes:
- redis_data_5:/data
redis-cluster:
image: redis/redis-stack:7.2.0-v15
container_name: *name-redis-cluster
command: >
sh -c '
echo "βŒ› Waiting for Redis nodes to be ready..." &&
sleep 10 &&
echo "πŸ”„ Creating Redis Cluster..." &&
redis-cli -a ${REDIS_PASSWORD} --cluster create redis-node-0:6379 redis-node-1:6379 redis-node-2:6379 redis-node-3:6379 redis-node-4:6379 redis-node-5:6379 --cluster-replicas 1 --cluster-yes &&
echo "πŸ” Verifying cluster state..." &&
until redis-cli -a ${REDIS_PASSWORD} -h redis-node-0 cluster info | grep -q "cluster_state:ok"; do
echo "⏳ Waiting for cluster to be ready..."
sleep 2
done &&
echo "βœ… Redis Cluster is ready and verified!"'
restart: "no"
depends_on:
redis-node-0:
condition: service_healthy
redis-node-1:
condition: service_healthy
redis-node-2:
condition: service_healthy
redis-node-3:
condition: service_healthy
redis-node-4:
condition: service_healthy
redis-node-5:
condition: service_healthy
# RedisInsight - Web administration interface
redis-insight:
image: redis/redisinsight:2.66
container_name: *name-redis-insight
ports:
- "5540:5540"
volumes:
- redis_insight:/data
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:5540/api/health/"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
redis-commander:
image: rediscommander/redis-commander:latest
container_name: *name-redis-commander
environment:
- REDIS_HOSTS=redis-node-0:redis-node-0:6379:0:${REDIS_PASSWORD},redis-node-1:redis-node-1:6379:0:${REDIS_PASSWORD},redis-node-2:redis-node-2:6379:0:${REDIS_PASSWORD},redis-node-3:redis-node-3:6379:0:${REDIS_PASSWORD},redis-node-4:redis-node-4:6379:0:${REDIS_PASSWORD},redis-node-5:redis-node-5:6379:0:${REDIS_PASSWORD}
ports:
- "8081:8081"
depends_on:
- redis-cluster
networks:
default:
name: *network-default
driver: bridge
volumes:
redis_data_0:
name: *vol-redis-0
redis_data_1:
name: *vol-redis-1
redis_data_2:
name: *vol-redis-2
redis_data_3:
name: *vol-redis-3
redis_data_4:
name: *vol-redis-4
redis_data_5:
name: *vol-redis-5
redis_insight:
name: ${COMPOSE_PROJECT_NAME:-projectx}-redis-insight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment