
Setting Up Redis with Docker Compose

Redis is a powerful in-memory data store that serves as a fundamental building block for modern applications, offering lightning-fast caching, session management, and real-time analytics capabilities. While Redis itself is incredibly powerful, managing it in a development or production environment can be challenging. This is where Docker Compose comes in – it allows you to define, configure, and run Redis in a containerized environment with just a few lines of code.
In this guide, you'll learn how to set up a production-ready Redis instance using Docker Compose. We'll cover everything from basic configuration to data persistence and security measures, making it perfect for both development and production environments.
Step 1: Create a Docker Compose File
First, you'll need to create a Docker Compose file. This file will define the services that make up your application environment. For our purpose, we'll focus on setting up Redis.
Open your text editor and create a file named docker-compose.yml
. Here's what you should put in it:
services:
cache:
image: redis:7.4-alpine
restart: always
ports:
- "6379:6379"
command: redis-server --save 20 1 --loglevel warning --requirepass yourpassword
volumes:
- cache:/data
volumes:
cache:
driver: local
Let's break down what each part does:
- services: Defines the services that make up your app. Here, we're defining a service named
cache
. You would also add other services here, like a database, a web server, etc. - image: Tells Docker Compose to use the Redis 7.4 image based on Alpine Linux.
- restart: Set to
always
, which means the container will restart if it stops or crashes. - ports: Maps port 6379 on your local machine to port 6379 in the container, allowing you to connect to Redis from your host machine.
- command: Customizes the Redis server command.
--save 20 1
tells Redis to save the database every 20 seconds if at least one change was made.--loglevel warning
sets the logging to show only warnings.--requirepass yourpassword
sets a password for Redis, which is a basic security measure. Replaceyourpassword
with a strong password. - volumes: Configures a volume named
cache
and maps it to/data
inside the container. This ensures that data is persisted even if the container is deleted or recreated.
Step 2: Start the Redis Service
Once your docker-compose.yml
file is ready, you can start the Redis service with the following command in your terminal:
docker-compose up -d
The -d
flag runs the containers in the background. You should see output indicating that the Redis service is up and running.
Step 3: Verify Redis is Running
To ensure Redis is running correctly, you can use the Redis command-line interface (CLI). First, find the name of your Redis container with:
docker-compose ps
Then, you can execute the Redis CLI inside the container with:
docker-compose exec cache redis-cli -a yourpassword
Replace yourpassword
with the password you set in the docker-compose.yml
file. Once in the CLI, you can issue commands like PING
to check if Redis is responding:
127.0.0.1:6379> PING
PONG
You can also set and get keys to test further:
127.0.0.1:6379> SET testkey "Hello, Redis!"
OK
127.0.0.1:6379> GET testkey
"Hello, Redis!"
Step 4: Stopping and Removing the Redis Service
When you're done, you can stop the Redis service with:
docker-compose down
This command will stop and remove the containers defined in your docker-compose.yml
file. Your volumes will be preserved!
By following these steps, you've set up Redis with Docker Compose, complete with persistence and basic security. This setup can be easily integrated into larger applications or used standalone for caching purposes.