Redis with Docker
17. Redis with Docker
Section titled “17. Redis with Docker”Running Redis in Docker
Section titled “Running Redis in Docker”# Pull official Redis image and rundocker run --name my-redis \ -p 6379:6379 \ -d redis:latest
# Verify it is runningdocker ps
# Access Redis CLI inside the containerdocker exec -it my-redis redis-cli
# Test127.0.0.1:6379> PINGPONGRedis with Persistence Volume
Section titled “Redis with Persistence Volume”# Run Redis with a volume for data persistencedocker run --name my-redis \ -p 6379:6379 \ -v /my/local/path:/data \ -d redis redis-server --appendonly yesDocker Compose (Redis + Node.js App)
Section titled “Docker Compose (Redis + Node.js App)”version: '3.9'
services: redis: image: redis:7-alpine container_name: redis ports: - "6379:6379" volumes: - redis_data:/data command: redis-server --appendonly yes healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s timeout: 5s retries: 5
app: build: . container_name: node-app ports: - "3000:3000" environment: - REDIS_HOST=redis - REDIS_PORT=6379 depends_on: redis: condition: service_healthy
volumes: redis_data:# Start the stackdocker compose up -d
# View Redis logsdocker logs redis -f
# Stop everythingdocker compose downRedis with Custom Configuration
Section titled “Redis with Custom Configuration”# redis.confmaxmemory 256mbmaxmemory-policy allkeys-lruappendonly yesrequirepass your_strong_password# Run with custom configdocker run --name my-redis \ -p 6379:6379 \ -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf \ -d redis redis-server /usr/local/etc/redis/redis.conf