One primary use of Redis is caching and storage in large applications. For example, we can use Redis as a caching mechanism for relational databases. Redis is also versatile which supports many data structures such as strings, lists, sets, hashes, etc.
Finally, Redis offers data persistence, pub-sub messaging, atomic operations, clustering, replication, and many more features that make it a compelling choice for various applications.
One of the best ways to run a Redis server in your environment is using the Docker containers. In this tutorial, we will learn how to use the Docker Compose to create a running instance of the Redis server on our machine.
To follow along with this tutorial, ensure that you have the following:
- Installed Docker and Docker Compose
- Sudo or administrative permissions on the host system
- Network connectivity
Once you have the given requirements met, we can proceed and discuss how to configure Redis using Docker Compose.
What Is Docker Compose?
If you are unfamiliar, Docker Compose is a tool that is part of the Docker ecosystem that allows us to define and run the multi-container applications using the Docker engine.
Docker Compose simplifies the process of managing and deploying a complex application environment by providing a declarative language using YAML to define all the requirements and structure of the environment.
For example, using Docker Compose, we can define the requirements of an application starting from the web server, the database server, network configuration, load balancers, and more.
Running Redis with Docker Compose
Let us proceed and learn how to run Redis with Docker Compose. The first step is to create a new directory to store the configuration for our project:
$ cd redis-docker
The next step is to create a new Docker Compose file named “docker-compose.yml” file in the project directory.
We use this file to define the features and the configuration of the Redis server that we wish to include:
Edit this file with an editor of your choice:
In the configuration file, add the entries as shown in the following:
In the previous configuration, we define a service named “Redis” based on the official Redis docker image provided by Bitnami.
We then map the default Redis port 6379 to the same host port on the container. This allows us to access the running Redis instance from the host machine on the same port.
The next step is to add persistence to our container. Add the following entries to the configuration file:
In the previous conf, we define a new volume called “redis_data” which we use to store the Redis data inside the container. We set the path to “/bitnami/redis/data”.
Adding a named volume to the data container ensures the data persistence even if the container is stopped or removed which is essential for a database server.
The next step is defining some configuration variables that govern how the Redis server behaves.
In our case, we want to allow the empty passwords and turn off some dangerous commands on the server. We can do this using the environment block as shown in the following basic configuration:
As you can guess, we set Redis to allow the empty passwords for authentication and to disable the use of FLUSHDB and FLUSHALL commands which can be used to remove all the data that are stored in the server.
We can save and close the file once we are satisfied with the configuration. Keep in mind that the configuration provided in this post is basic; there is more you can do to customize your Redis server as outlined in the documentation. Feel free to reference the source material to learn more.
Once satisfied, we can run the Docker container using the following command:
This should start the container in the background.
That’s it! You successfully learned how to define and run a Docker Compose file for a Redis instance. As you discovered in this post, using the Docker Compose makes managing and running the Redis containers with consistent settings across different environments easy and efficient.