Introduction
When running Redis in a Docker Compose setup, you need to use docker compose exec to run redis-cli inside the Redis container. This lets you interact with the Redis instance for debugging, manual data manipulation, monitoring, and testing — without installing Redis on your host machine.
Basic Docker Compose Setup
1# docker-compose.yml
2services:
3 redis:
4 image: redis:7-alpine
5 ports:
6 - "6379:6379"
7 volumes:
8 - redis_data:/data
9
10volumes:
11 redis_data:
Start the service:
Connecting with redis-cli
Interactive Mode
1# Open an interactive redis-cli session
2docker compose exec redis redis-cli
3
4# You'll see the Redis prompt:
5# 127.0.0.1:6379>
Run a Single Command
1# Ping the server
2docker compose exec redis redis-cli PING
3# PONG
4
5# Set a key
6docker compose exec redis redis-cli SET mykey "hello"
7# OK
8
9# Get a key
10docker compose exec redis redis-cli GET mykey
11# "hello"
12
13# Check server info
14docker compose exec redis redis-cli INFO server
Common redis-cli Commands
1# Check connection
2docker compose exec redis redis-cli PING
3
4# Set and get key-value pairs
5docker compose exec redis redis-cli SET user:1 '{"name":"Alice","age":30}'
6docker compose exec redis redis-cli GET user:1
7
8# List all keys (use cautiously in production)
9docker compose exec redis redis-cli KEYS '*'
10
11# Delete a key
12docker compose exec redis redis-cli DEL mykey
13
14# Check key TTL
15docker compose exec redis redis-cli TTL mykey
16
17# Set key with expiration (60 seconds)
18docker compose exec redis redis-cli SETEX session:abc 60 "token123"
19
20# Monitor all commands in real-time
21docker compose exec redis redis-cli MONITOR
22
23# Get server stats
24docker compose exec redis redis-cli INFO memory
25docker compose exec redis redis-cli DBSIZE
Connecting from Another Container
If your application container needs to connect to Redis:
1# docker-compose.yml
2services:
3 redis:
4 image: redis:7-alpine
5 ports:
6 - "6379:6379"
7
8 app:
9 build: .
10 depends_on:
11 - redis
12 environment:
13 - REDIS_HOST=redis
14 - REDIS_PORT=6379
From the app container, use the service name redis as the hostname:
1# Connect from the app container
2docker compose exec app redis-cli -h redis -p 6379
3
4# Or if redis-cli isn't in the app container:
5docker compose exec redis redis-cli
Redis with Authentication
1services:
2 redis:
3 image: redis:7-alpine
4 command: redis-server --requirepass mysecretpassword
5 ports:
6 - "6379:6379"
Connect with password:
1# Method 1: -a flag
2docker compose exec redis redis-cli -a mysecretpassword
3
4# Method 2: AUTH command after connecting
5docker compose exec redis redis-cli
6# 127.0.0.1:6379> AUTH mysecretpassword
7# OK
8
9# Method 3: REDISCLI_AUTH environment variable
10docker compose exec -e REDISCLI_AUTH=mysecretpassword redis redis-cli
Custom Redis Configuration
1services:
2 redis:
3 image: redis:7-alpine
4 command: redis-server /usr/local/etc/redis/redis.conf
5 volumes:
6 - ./redis.conf:/usr/local/etc/redis/redis.conf
7 - redis_data:/data
8 ports:
9 - "6379:6379"
10
11volumes:
12 redis_data:
Example redis.conf:
1# redis.conf
2maxmemory 256mb
3maxmemory-policy allkeys-lru
4appendonly yes
5requirepass mysecretpassword
Connecting from Host Machine
If you exposed port 6379, you can connect from the host (if redis-cli is installed locally):
1# From host machine
2redis-cli -h localhost -p 6379
3
4# With password
5redis-cli -h localhost -p 6379 -a mysecretpassword
Or without installing redis-cli, run it in a temporary container:
docker run --rm -it --network host redis:7-alpine redis-cli -h localhost -p 6379
Useful Debugging Commands
1# Memory usage of a specific key
2docker compose exec redis redis-cli MEMORY USAGE mykey
3
4# Slow log — commands that took too long
5docker compose exec redis redis-cli SLOWLOG GET 10
6
7# Connected clients
8docker compose exec redis redis-cli CLIENT LIST
9
10# Flush all data (CAUTION)
11docker compose exec redis redis-cli FLUSHALL
12
13# Save snapshot
14docker compose exec redis redis-cli BGSAVE
15
16# Check persistence status
17docker compose exec redis redis-cli LASTSAVE
Health Check
Add a health check to your compose file:
1services:
2 redis:
3 image: redis:7-alpine
4 ports:
5 - "6379:6379"
6 healthcheck:
7 test: ["CMD", "redis-cli", "ping"]
8 interval: 10s
9 timeout: 5s
10 retries: 3
Common Pitfalls
exec vs run: Use docker compose exec for running containers. docker compose run creates a new container, which may not be connected to the same network or data volume.
Service name matters: The first argument after exec is the service name from docker-compose.yml (e.g., redis), not the container name or image name.
Container not running: exec fails if the container is not running. Check with docker compose ps and start with docker compose up -d.
Port conflicts: If port 6379 is already in use on the host (local Redis installation), either stop the local Redis or change the host port mapping: "6380:6379".
Data persistence: Without a volume mount, Redis data is lost when the container is removed. Always use a named volume (redis_data:/data) or bind mount for production data.
KEYS command in production: KEYS * blocks Redis and scans all keys. Use SCAN for production: docker compose exec redis redis-cli --scan --pattern 'user:*'.
Summary
Use docker compose exec redis redis-cli to open an interactive Redis session
Use docker compose exec redis redis-cli COMMAND for single commands
Connect from other containers using the service name (redis) as the hostname
Add -a password or AUTH command when Redis requires authentication
Mount a volume for data persistence and a config file for custom settings
Use SCAN instead of KEYS in production to avoid blocking