Redis on Your Local Machine Using Docker: A Step-by-Step Guide 🚀
Nilesh Raut

Nilesh Raut @speaklouder

About: Software engineer @jio , Founder of Nileshblog.tech & Technilesh.com and More.

Location:
India
Joined:
Feb 15, 2023

Redis on Your Local Machine Using Docker: A Step-by-Step Guide 🚀

Publish Date: Apr 3
11 0

What is Redis and Why Should We Use It?

Imagine a super-fast assistant that remembers everything instantly. That’s Redis! It’s an in-memory data store used as a database, cache, and message broker. Developers love Redis for its lightning speed, simple commands, and scalability.

Now, the big question—why use Docker to run Redis locally? Well, setting up Redis manually can be tricky. With Docker, we skip the installation hassle and get Redis running with just a few commands. It’s like having Redis ready in a takeout box—just unpack and start using it!

Prerequisites: What You Need Before Starting

Before we dive in, let’s make sure we have everything ready:

  • Docker Installed: If you don’t have it, download and install Docker from the official website.
  • Basic Command Line Knowledge: No need to be a pro, but knowing how to run a few commands in the terminal will help.

Once Docker is up and running, we’re good to go!

Step 1: Pull the Redis Docker Image

First things first, we need to download the Redis image from Docker Hub. Think of it as getting a Redis blueprint that Docker will use to create a container.

Run this command:

docker pull redis:latest
Enter fullscreen mode Exit fullscreen mode

This command grabs the latest Redis version. If you need a specific version, replace latest with a version number, like redis:7.0.

Step 2: Run Redis in a Docker Container

Now, let’s bring Redis to life inside a Docker container. We’ll run:

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

What does this command do?

  • -d runs Redis in the background.
  • --name redis names our container "redis".
  • -p 6379:6379 makes Redis accessible on port 6379.

Boom! Redis is now running inside Docker. Let’s check if it’s up.

Step 3: Verify Redis is Running

To see if our Redis container is active, use:

docker ps
Enter fullscreen mode Exit fullscreen mode

This will list all running containers. If you see Redis in the list, congratulations! It’s alive and kicking.

Want to check if Redis is responding? Run:

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

You should get a response:

PONG
Enter fullscreen mode Exit fullscreen mode

That’s Redis saying, "Hey, I’m here!" 🚀

Step 4: Persisting Redis Data

By default, when the container stops, Redis loses its data. To keep our data safe, we need to store it on our machine using a volume.

Run Redis with data persistence:

docker run -d --name redis -p 6379:6379 -v ~/redis_data:/data redis:latest redis-server --save 60 1
Enter fullscreen mode Exit fullscreen mode

This ensures data is saved every 60 seconds if at least one change is made.

Step 5: Secure Redis with a Password

Redis, by default, doesn’t require a password. But security matters! Let’s add a password.

docker run -d --name redis -p 6379:6379 redis:latest redis-server --requirepass "your_secure_password"
Enter fullscreen mode Exit fullscreen mode

Now, when connecting to Redis, we must authenticate:

docker exec -it redis redis-cli
AUTH your_secure_password
Enter fullscreen mode Exit fullscreen mode

If the password is correct, Redis will grant access.

Step 6: Use Redis in a Real Project

Now that Redis is running, we can integrate it into applications. Let’s see an example using Node.js:

  1. Install Redis client for Node.js:
npm install redis
Enter fullscreen mode Exit fullscreen mode
  1. Connect to Redis and set a key-value pair:
const redis = require('redis');

const client = redis.createClient({
  url: 'redis://:your_secure_password@localhost:6379'
});

client.connect()
  .then(async () => {
    await client.set('greeting', 'Hello, Redis!');
    const value = await client.get('greeting');
    console.log(value); // Output: Hello, Redis!
    client.quit();
  })
  .catch(err => console.error('Redis Client Error', err));
Enter fullscreen mode Exit fullscreen mode

Bonus: Redis Insight for a Visual Interface

Want a graphical tool to manage Redis? Try Redis Insight!

Install and run Redis Insight:

docker run -d --name redisinsight -p 5540:5540 redis/redisinsight:latest
Enter fullscreen mode Exit fullscreen mode

Now, open http://localhost:5540 in your browser and connect to Redis for a visual experience.

Final Thoughts

And there you have it! We’ve successfully set up Redis on our local machine using Docker. From pulling the image to securing it with a password and using it in a project, we covered it all.

If you're interested in diving deeper into software development, check out this comprehensive guide.

Want to level up in frontend development? Explore this guide on React JS and Tailwind here.

Happy coding! 🚀

Comments 0 total

    Add comment