Redis is a lightning-fast, in-memory key-value store that’s perfect for caching, sessions, queues, and more. Docker makes it incredibly easy to spin up Redis in seconds without installing it on your system.
In this guide, we’ll walk through how to run Redis in Docker, persist data, and connect your application.
📦 Why Use Redis with Docker?
- 🧼 No need to install Redis directly on your system.
- ⏱️ Start/stop Redis with a single command.
- 🔁 Easily recreate and scale environments.
- 💾 Volume support for persistent data.
🛠️ Prerequisites
- Docker installed (Install from Docker)
- Basic terminal/command line experience
🧰 Step 1: Pull the Redis Image
docker pull redis
🧪 Step 2: Run Redis in a Container
docker run --name my-redis -p 6379:6379 redis
✅ Redis is now running on localhost:6379
.
🧠 Step 3: Interact with Redis CLI
docker exec -it my-redis redis-cli
Try some commands:
set name "DockerRedis"
get name
💾 Step 4: Add Data Persistence
docker run -d --name redis-persistent \
-p 6379:6379 \
-v redis_data:/data \
redis redis-server --appendonly yes
🔗 Step 5: Connect Your App to Redis
Python:
import redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set("foo", "bar")
print(r.get("foo"))
Node.js:
import { createClient } from 'redis';
const client = createClient();
await client.connect();
await client.set('foo', 'bar');
console.log(await client.get('foo'));
🧼 Step 6: Stop and Clean Up
docker stop my-redis
docker rm my-redis
Remove the volume (if used):
docker volume rm redis_data
🎯 Wrapping Up
Using Redis with Docker is a clean and efficient way to integrate caching, sessions, and more into your application. Perfect for both dev and production use cases.
🔗 Resources
✨ Happy Dockering with Redis!
Top comments (0)