Calling redis-cli in docker-compose setup
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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
Start the service:
Connecting with redis-cli
Interactive Mode
Run a Single Command
Common redis-cli Commands
Connecting from Another Container
If your application container needs to connect to Redis:
From the app container, use the service name redis as the hostname:
Redis with Authentication
Connect with password:
Custom Redis Configuration
Example redis.conf:
Connecting from Host Machine
If you exposed port 6379, you can connect from the host (if redis-cli is installed locally):
Or without installing redis-cli, run it in a temporary container:
Useful Debugging Commands
Health Check
Add a health check to your compose file:
Common Pitfalls
execvsrun: Usedocker compose execfor running containers.docker compose runcreates a new container, which may not be connected to the same network or data volume.- Service name matters: The first argument after
execis the service name fromdocker-compose.yml(e.g.,redis), not the container name or image name. - Container not running:
execfails if the container is not running. Check withdocker compose psand start withdocker 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. UseSCANfor production:docker compose exec redis redis-cli --scan --pattern 'user:*'.
Summary
- Use
docker compose exec redis redis-clito open an interactive Redis session - Use
docker compose exec redis redis-cli COMMANDfor single commands - Connect from other containers using the service name (
redis) as the hostname - Add
-a passwordorAUTHcommand when Redis requires authentication - Mount a volume for data persistence and a config file for custom settings
- Use
SCANinstead ofKEYSin production to avoid blocking
Related reading
- Can a reference to an element inside an stdmap be invalidated?
- Can dockerfile be put in .dockerignore?
- Can I modify container's environment variables without restarting pod using kubernetes
- Can I run Docker Desktop on Windows without admin privileges?
- Can 2 Debezium Connectors read from same source at the same time?
- Can a database be replicated to an EC2 instance from outside Amazon?
- Can a model be created on Spark batch and use it in Spark streaming?
- Can a model trained on gpu used on cpu for inference and vice versa?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.