When it comes to in-memory data stores, Redis stands out as a go-to solution for developers needing speed, simplicity, and scalability. Whether you're caching database queries, managing session data, or creating a real-time leaderboard, Redis gets the job done fast. But setting it up from scratch can be a hassle—unless you're using Docker.
In this guide, we’ll walk you through how to install and run Redis using Docker. This method is especially useful for local development or quick testing environments. No need to mess with complex configurations or installations—just pull, run, and go.
Why Use Docker for Redis?
Docker simplifies software delivery by packaging applications and their dependencies into containers. Here's why Docker is a great fit for running Redis:
- Quick setup: You can have Redis up and running with a single command.
- Clean environment: Avoid cluttering your system with extra services or dependencies.
- Portability: Easily share your Redis setup with teammates or deploy across machines.
- Reproducibility: Use the same setup on local, staging, and production systems.
Prerequisites
Before starting, make sure Docker is installed and running on your machine. You can grab Docker from the official website:
https://www.docker.com/products/docker-desktop
Step-by-Step: Installing Redis Using Docker
Step 1: Pull the Redis Docker Image
The official Redis image is hosted on Docker Hub. To download it, open your terminal and run:
docker pull redis
This command fetches the latest Redis image from Docker Hub. If you want a specific version, append the tag, like
redis:7.2
.
Step 2: Run the Redis Container
Now that the image is downloaded, start a new Redis container with:
docker run --name redis-container -p 6379:6379 -d redis
Let’s break that down:
--name redis-container
gives your container a friendly name.
-p 6379:6379
maps Redis’s default port from the container to your host machine.
-d
runs the container in the background (detached mode).
redis
is the image name we pulled earlier.
That’s it! Redis is now running and listening on port 6379.
Step 3: Verify Redis is Running
To check that everything is working, you can either:
- Use
docker ps
to confirm the container is active.
- Connect to Redis using the built-in CLI:
docker exec -it redis-container redis-cli
You’ll get a prompt like this:
127.0.0.1:6379>
Try a test command:
127.0.0.1:6379> PING
You should get a reply:
PONG
Congrats—you’re officially talking to Redis via Docker.
Optional: Using Docker Compose for Redis
If you want an even more organized setup, especially when using Redis alongside other services, Docker Compose is a great option.
Create a
docker-compose.yml
file like this:
version: '3'
services:
redis:
image: redis
container_name: redis-container
ports:
- "6379:6379"
Then run:
docker-compose up -d
This starts Redis in the background, and you can stop it with
docker-compose down
.
Customizing Redis Configuration
By default, Redis runs with its standard configuration. If you need to tweak it—like setting a password, changing memory limits, or enabling persistence—you can mount a custom config file:
docker run --name redis-container -p 6379:6379 -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf
This command tells Docker to use your custom configuration file inside the container.
Storing Data Persistently
By default, Docker containers are ephemeral, meaning all data is lost when the container is deleted. To keep your Redis data even after stopping or removing the container, mount a volume:
docker run --name redis-container -p 6379:6379 -v redis-data:/data -d redis
Docker will store Redis data in a named volume called
redis-data
.
image quote pre code