DEV Community

Cover image for From Zero to Redis Hero: Your First Redis Container with Docker
Chandrani Mukherjee
Chandrani Mukherjee

Posted on

From Zero to Redis Hero: Your First Redis Container with Docker

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
Enter fullscreen mode Exit fullscreen mode

🧪 Step 2: Run Redis in a Container

docker run --name my-redis -p 6379:6379 redis
Enter fullscreen mode Exit fullscreen mode

✅ Redis is now running on localhost:6379.


🧠 Step 3: Interact with Redis CLI

docker exec -it my-redis redis-cli
Enter fullscreen mode Exit fullscreen mode

Try some commands:

set name "DockerRedis"
get name
Enter fullscreen mode Exit fullscreen mode

💾 Step 4: Add Data Persistence

docker run -d --name redis-persistent \
  -p 6379:6379 \
  -v redis_data:/data \
  redis redis-server --appendonly yes
Enter fullscreen mode Exit fullscreen mode

🔗 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"))
Enter fullscreen mode Exit fullscreen mode

Node.js:

import { createClient } from 'redis';
const client = createClient();

await client.connect();
await client.set('foo', 'bar');
console.log(await client.get('foo'));
Enter fullscreen mode Exit fullscreen mode

🧼 Step 6: Stop and Clean Up

docker stop my-redis
docker rm my-redis
Enter fullscreen mode Exit fullscreen mode

Remove the volume (if used):

docker volume rm redis_data
Enter fullscreen mode Exit fullscreen mode

🎯 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)