Here is a 2500+ word blog post on how to run Redis in Docker:
Redis is a powerful in-memory data structure store that is commonly used for caching, queuing, and streaming use cases. Its high performance and versatility make it a popular choice for modern applications. However, setting up and managing a Redis server can be complex.
This is where Docker comes in. Docker provides a simple way to containerize applications like Redis, making deployment and management much easier. In this comprehensive guide, we will cover everything you need to know to run Redis in a Docker container.
Prerequisites
Before we begin, you‘ll need to have Docker installed on your system. Here are guides for installing Docker on some common operating systems:
- Install Docker on Ubuntu 20.04
- Install Docker on Ubuntu 22.04
- Install Docker on CentOS 8
- Install Docker on Debian 11
I also recommend having a basic understanding of Docker concepts like images, containers, Dockerfile, docker-compose etc. Here is a great Docker tutorial for beginners.
Once Docker is setup, we can start containerizing Redis!
Step 1 – Pull the Official Redis Image
We will use the official Redis image from Docker Hub to create our container. This contains Ubuntu 20.04 with Redis pre-installed and configured.
To download the image, run:
docker pull redis
This will pull down the latest version of the redis image. You can check that the image is now available locally using:
docker images
You should see output similar to:
REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 57092d84af3d 2 days ago 113MB
Step 2 – Start a Redis Container
With the image downloaded, we can now launch a Redis container.
Below is the command I will use to start the container:
docker run --name redis-server -p 6379:6379 -d redis
Let‘s examine each part of this command:
- docker run – The docker run command starts a new container from an image
- –name redis-server – Name the container "redis-server" for easy identification
- -p 6379:6379 – Map port 6379 in the container to port 6379 on the host machine. This exposes Redis on its standard port.
- -d – Run the container in detached mode (in the background)
- redis – Use the redis image to start the container
After running the above command, our Redis container will be started and running in the background.
You can verify it is running with docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f31ccb4ed2f8 redis "docker-entrypoint.s..." About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp redis-server
Awesome! Our Redis container is now ready to accept connections on port 6379.
Step 3 – Test the Redis Connection
Let‘s confirm that Redis is working properly by connecting to the server and running a simple command.
First, start an interactive shell session inside the container:
docker exec -it redis-server sh
This will give us CLI access for running Redis commands.
Now connect to the server using the redis-cli client:
# redis-cli 127.0.0.1:6379> pingPONG
The PONG response confirms that our connection was successful and Redis is accepting requests!
You can now use any Redis commands like SET, GET, LPUSH etc to start populating data.
To exit the redis-cli, type exit to return to the container shell prompt. Then exit again to close the shell session completely.
Step 4 – Stop and Remove the Container
When ready to tear down the environment, you first need to stop the running container.
Get the CONTAINER ID from docker ps, then stop it with:
docker stop CONTAINER_ID
Once stopped, the container can be removed:
docker rm CONTAINER_ID
And just like that, our Redis container is deleted and all resources freed up!
Docker Volumes for Persistent Storage
One downside of the above method is that any data stored in Redis will be lost when the container shuts down.
For persisting Redis data across container restarts, we should mount a volume.
Docker volumes provide persistent storage for containers by mapping a folder on the host machine to a folder within the container. Even if the container is removed, any data written to the volume will still be saved.
Here is the updated docker run command to add a volume:
docker run --name redis-server -p 6379:6379 -v redis-data:/data -d redis
The new -v redis-data:/data flag mounts a volume called redis-data from the host into the /data directory inside the Redis container.
Any Redis dumps, append-only files (AOF) etc can be persisted here so data is not lost between container restarts.
Using Docker Compose
Manually running Docker commands can be tedious. For multi-container environments, Docker Compose is a better option.
Docker Compose uses a YAML formatted file to configure application services and networking. Just one command can launch everything defined in your docker-compose.yml.
Here is an example docker-compose.yml for running Redis:
version: ‘3‘
services:
redis:
image: redis:latest
ports:
- ‘6379:6379‘
volumes:
- redis-data:/data
volumes:
redis-data:
Save this as docker-compose.yml then run:
docker-compose up -d
This will start Redis just like our earlier example, but also creates the persistent volume for you automatically.
Much easier than multiple docker commands! You can add additional services to this file like databases, queues etc.
To stop the environment:
docker-compose down
And that deletes all containers while persistently storing data in the redis-data volume.
Optimizing the Redis Docker Image
So far we have used the default Redis docker image without modification. But for production deployments, optimizing the image can improve security, efficiency and performance.
Here are some best practices for running Redis in Docker:
Use a Minimal Base Image
The official Redis repo is built on top of Ubuntu – a very large base image.
Alpine Linux provides a much smaller base container image. Here is an Alpine based Redis image:
docker pull redis:alpine
At under 6MB in size, Alpine enables faster pulls and boots while using fewer system resources.
Enable Protected Mode
Modify the docker-entrypoint.sh script to uncomment this line to enable protected mode:
protected-mode yes
This provides greater security by restricting access from external clients and requires the use of an authentication password.
Deny access to Redis CONFIG and DEBUG commands
Add this line to disallow potentially dangerous CONFIG and DEBUG commands:
rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
Set Memory Limits
By default, Redis can grow to use all available system memory. This can cause issues on containers with dynamic workloads.
Explicitly set a memory limit under docker run:
docker run --memory="200m" redis
Or in a Dockerfile:
MEM_LIMIT=209715200
Use Non-Default Ports
Changing the default Redis port provides some additional security through obscurity.
For example, map your host port to 16379 instead of standard 6379 port.
Conclusion
In this detailed guide, we covered how to effectively run Redis in Docker, using the official image from Docker Hub.
We explored methods to:
- Pull and run a Redis image
- Connect a Redis client
- Persist storage with volumes
- Simplify management with Docker Compose files
- Improve security through custom optimization
Containerizing software like Redis simplifies deployment, while providing portability and resource control.
With the instructions in this article, you now have all the tools to get Redis up and running in Docker for development or production systems.
I hope this guide was informative and helps you utilize the power of Redis and containers. Let me know in the comments if you have any other tips for running Redis in Docker!


