✨ New update: Automation 2.0 is live — smarter workflows, faster results.

Redis on Docker: A Step-by-Step Guide to Running Redis in Containers

Introduction Docker is a widely used platform for developing, shipping, and running applications within containers. By utilizing Docker, developers can simplify the deployment process, improve application scalability, and ensure consistent runtime environments. In this article, we will provide a step-by-step guide on running Redis in Docker containers, covering installation, configuration, and basic management of Redis …

Introduction

Docker is a widely used platform for developing, shipping, and running applications within containers. By utilizing Docker, developers can simplify the deployment process, improve application scalability, and ensure consistent runtime environments.

In this article, we will provide a step-by-step guide on running Redis in Docker containers, covering installation, configuration, and basic management of Redis containers.

Prerequisites

Before we begin, ensure that you have the following installed on your system:

Pulling the Redis Image from Docker Hub

First, we need to pull the official Redis image from Docker Hub. This image contains the Redis server and all necessary dependencies. Open a terminal and run the following command:

Docker
docker pull redis

Docker will download the Redis image and store it locally on your system.

Running a Redis Container

To run a Redis container using the downloaded image, execute the following command:

Docker
docker run --name my-redis -p 6379:6379 -d redis

This command creates and starts a new Redis container named “my-redis” and maps port 6379 on your host to port 6379 on the container. The -d flag runs the container in detached mode, allowing it to run in the background.

Connecting to the Redis Container

To connect to the running Redis container, you can use the docker exec command to run the Redis CLI:

Docker
docker exec -it my-redis redis-cli

This command opens an interactive shell connected to the Redis server running inside the “my-redis” container. You can now execute Redis commands, such as SET, GET, and DEL, directly from the CLI.

Configuring Redis in Docker

By default, Redis in Docker uses the default configuration provided by the Redis image. However, you may need to customize the Redis configuration to suit your needs. To do this, create a new configuration file on your host system and mount it as a volume in the Redis container.

Example: Create a new configuration file named “redis.conf” with the following content:

maxmemory 256mb
maxmemory-policy allkeys-lru

Now, run a new Redis container with the custom configuration:

docker run --name my-redis-custom -p 6380:6379 -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf -d redis redis-server /usr/local/etc/redis/redis.conf

This command maps the local “redis.conf” file to “/usr/local/etc/redis/redis.conf” inside the container and instructs the Redis server to use this configuration file.

Managing Redis Containers

Here are some useful Docker commands for managing your Redis containers:

  • Listing running containers: docker ps
  • Stopping a container: docker stop
  • Starting a stopped container: docker start
  • Removing a container: docker rm
  • Viewing container logs: docker logs

Persisting Redis Data in Docker

By default, Redis containers store data in memory and do not persist data to disk. To enable data persistence, you can mount a volume from your host system to the Redis container.

Example: Create a directory named “redis-data” on your host system and mount it as a volume in the Redis container:

Docker
docker run --name my-redis-persistent -p 6390:6379 -v /path/to/redis-data:/data -d redis

This command maps the local “redis-data” directory to “/data” inside the container. Redis will now persist data to this directory using the default RDB snapshotting method.

To use AOF persistence instead of RDB, update your custom configuration file (e.g., “redis.conf”) to enable AOF:

Then, run the Redis container with both the configuration file and data volume mounted:

Docker
docker run --name my-redis-persistent-aof -p 6391:6379 -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf -v /path/to/redis-data:/data -d redis redis-server /usr/local/etc/redis/redis.conf

Setting up a Redis Cluster in Docker

To set up a Redis cluster using Docker, you can use the official Redis image and create multiple Redis containers with the appropriate configuration. Here is an example of creating a simple Redis cluster with three master nodes and three replica nodes.

  1. Create a new directory named “redis-cluster” and a “redis.conf” file with the following content:
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000

2. Create a Docker network for the Redis cluster:

Docker
docker network create redis-cluster-network

3. Run six Redis containers (three masters and three replicas) with the custom configuration and network:

for i in {1..6}; do
docker run -d --name redis-node-$i
--network redis-cluster-network
-v /path/to/redis-cluster/redis.conf:/usr/local/etc/redis/redis.conf
redis redis-server /usr/local/etc/redis/redis.conf
done

4. Obtain the IP addresses of the Redis containers:

for i in {1..6}; do
docker inspect -f '{{ .NetworkSettings.Networks.redis-cluster-network.IPAddress }}' redis-node-$i
done

5. Use the Redis CLI to create the cluster:

redis-cli --cluster create :6379 :6379 :6379 :6379 :6379 :6379 --cluster-replicas 1

Replace to with the IP addresses obtained in the previous step.

Conclusion

In this article, we have covered the step-by-step process of running Redis in Docker containers, from installation and configuration to management and advanced use cases like data persistence and clustering. By using Docker to deploy and manage your Redis instances, you can streamline the deployment process, ensure consistent runtime environments, and easily scale your Redis infrastructure.

ali.akhwaja@gmail.com

ali.akhwaja@gmail.com

Related Posts

Kafka is widely used message broker especially in distributed systems, many ask this question that why Kafka is preferred over other available message brokers. There is no clear answer to this question instead it depends on your own requirements. Here we will discuss fundamentals of Kafka which you should know to get started. What is …

Software project management is an art and science of planning and leading software projects. It is a sub-discipline of project management in which software projects are planned, implemented, monitored and controlled. A software project manager is the most important person inside a team who takes the overall responsibilities to manage the software projects and play …

Leave a Reply

Your email address will not be published. Required fields are marked *