Running Redis in Docker

Learn how to run Redis in Docker with persistence, password authentication, custom configuration, and memory limits. Covers common caching and session store patterns.

Real-World Stacks

Detailed Explanation

Redis as a Dockerized Cache

Redis is frequently used as an in-memory cache, session store, and message broker. The official Docker image makes it straightforward to run:

docker run -d --name redis -p 6379:6379 redis:7-alpine

This starts Redis with the default configuration: no password, no persistence, listening on port 6379.

Adding Password Authentication

docker run -d \
  --name redis \
  -p 6379:6379 \
  redis:7-alpine \
  redis-server --requirepass mysecretpassword

Clients must authenticate with AUTH mysecretpassword before executing commands. In a connection URL, this looks like redis://:mysecretpassword@localhost:6379.

Enabling Data Persistence

Redis supports two persistence mechanisms:

RDB snapshots (periodic point-in-time snapshots):

docker run -d \
  --name redis \
  -v redis-data:/data \
  -p 6379:6379 \
  redis:7-alpine \
  redis-server --save 60 1000 --save 300 100

This saves a snapshot every 60 seconds if at least 1000 keys changed, or every 300 seconds if at least 100 keys changed.

AOF (Append Only File) for better durability:

docker run -d \
  --name redis \
  -v redis-data:/data \
  -p 6379:6379 \
  redis:7-alpine \
  redis-server --appendonly yes --appendfsync everysec

AOF logs every write operation, providing better durability at the cost of more disk I/O.

Memory Limits

Redis should be configured with a maximum memory limit to prevent it from consuming all available RAM:

docker run -d \
  --name redis \
  --memory 512m \
  -v redis-data:/data \
  -p 6379:6379 \
  redis:7-alpine \
  redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru

Note: The Docker memory limit (--memory 512m) should be higher than the Redis maxmemory to leave room for Redis overhead and the operating system.

Eviction Policies

Policy Behavior
noeviction Return error when memory limit is reached
allkeys-lru Remove least recently used keys (recommended for caches)
volatile-lru Remove LRU keys with expiration set
allkeys-random Remove random keys
volatile-ttl Remove keys closest to expiration

Custom Configuration File

Mount a custom redis.conf for full control:

docker run -d \
  --name redis \
  -v redis-data:/data \
  -v $(pwd)/redis.conf:/usr/local/etc/redis/redis.conf:ro \
  -p 6379:6379 \
  redis:7-alpine \
  redis-server /usr/local/etc/redis/redis.conf

Production Docker Run

docker run -d \
  --name redis \
  --restart unless-stopped \
  --memory 1g \
  --cpus 1 \
  --network app-net \
  -v redis-data:/data \
  redis:7-alpine \
  redis-server \
    --requirepass "${REDIS_PASSWORD}" \
    --maxmemory 768mb \
    --maxmemory-policy allkeys-lru \
    --appendonly yes \
    --appendfsync everysec

Connecting from Application Containers

docker network create app-net

docker run -d --name redis --network app-net redis:7-alpine

docker run -d \
  --name app \
  --network app-net \
  -e REDIS_URL=redis://redis:6379 \
  my-app-image

Use Case

Deploying Redis as a high-performance caching layer for a web application, with password authentication, LRU eviction, and persistent storage to survive container restarts.

Try It — Docker Run Command Builder

Open full tool