How to Set Up Redis using Docker Compose

This tutorial will show you how to set up and run Redis on your machine using Docker Compose.

Redis Docker

Redis is a free and open-source in-memory data store known for its incredibly low latency read and writes. Thanks to its speed, it has become one of the most used NoSQL databases in the world, often being utilized as a cache.

One of the advantages of using Redis over something like Memcached is that you can also store data on the disk. This allows reads and writes to remain persistent even if the software restarts. It also will enable Redis to only keep the most used items in memory.

By using Docker, you can get Redis up and running with just a few short commands. Not only does Docker make running the server easy, but it also makes it super easy to update and ensure it keeps running.

Over the following sections, we will show you how to install Docker on your machine and then write a Compose file to manage and run Redis.

The steps below are focused on Linux-based systems, but the same Compose file should also work on other systems.

Installing and Running Redis using Docker Compose

The following steps will show you how to get Redis up and running using Docker Compose.

This process is relatively simple and involves installing Docker and writing a simple Compose file to pull the Redis image.

Setting up Docker on your Device

1. Before we can run Redis using Docker, you must have the Docker runtime installed on your machine. If you already have Docker installed, skip to step 4.

Using the following command, we can use curl to grab the official Docker install script and pipe it into the terminal. This script makes installing Docker super simple.

curl -sSL https://get.docker.com | sh

2. Once you have Docker installed, you will want to add your current user to the Docker group. Adding your user will allow you to manage your Redis container without using the superuser.

To add your user to this group, use the command below within the terminal.

sudo usermod -aG docker $USER

3. Changes to your user’s groups require you to log out and log back in.

If you are using SSH or a terminal-only session, you can log out using the following command.

logout

Alternatively, restarting your device will also ensure that the groups get updated.

sudo reboot

Creating a Folder to Store your Redis Docker Container

4. With Docker installed, you will want to create a directory to store your Redis Docker container’s compose file and its data files.

We will create directory at “/opt/stacks/redisusing the mkdir command. The “-p” option allows us to create the full path regardless of whether a folder is missing.

sudo mkdir -p /opt/stacks/redis

5. The rest of the tutorial will expect us to be within this directory we created.

You can move to this directory using the following command within the terminal.

cd /opt/stacks/redis

Writing a Docker Compose File to Run Redis

6. We are now at the point where we can write a Docker compose file to run Redis on your system.

A compose file is a set of instructions that Docker will use to run your container. It is simpler to manage than using individual commands.

sudo nano compose.yaml

7. Within this file, you will want to enter the following lines.

The main thing that you will want to do here is replace “<PASSWORD>” with the password you want to use to interact with your Redis Docker container. Setting a password is crucial as it stops anyone from accessing your database.

In addition to setting a password requirement, we also adjust the launch command to use the “save” option. This option tells your Redis server that it should save the contents of your database to the disk every 60 seconds.

version: '3.8'
services:
  redis:
    image: redis:6.2-alpine
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --save 60 1 --loglevel warning --requirepass <PASSWORD>
    volumes: 
      - ./data:/data

8. After filling out the Compose file, you can save and quit by pressing CTRL + X, Y, and then ENTER.

Starting up Redis using Docker

9. With the Compose file written, all you need to do to start up Redis using Docker is to use the following command.

This command will launch Redis using all of the information provided within the Compose file. When it starts the container, it must download the Redis image, which might take a couple of minutes to complete.

You will only need to run this once to start up your server. Docker will automatically start Redis every time your system restarts.

docker compose up

At this point, you can start using the Redis server. If you want to verify that everything is working, you can follow the next section, but it is absolutely not required.

If you are having trouble connecting, ensure that you specify the same password you wrote within the Compose file earlier.

Testing your new Redis Container

10. With the container up and running, we can test that the Redis server is working correctly.

The easiest way to do this is to install the “redis-tools” package. The following step will differ slightly for those running a system that doesn’t use the “apt” package manager.

sudo apt install redis-tools

11. With the above package installed, we can launch the Redis command line interface using the following command. This tool will allow us to connect to and test the server.

redis-cli

12. Now that we are in the Redis CLI it should attempt to connect automatically to the “local” Redis server running within the Docker container.

Before you can perform any tasks, you will need to authenticate yourself.

Authenticating with the Redis server is as simple as typing in the following command. Ensure you replace “<PASSWORD>” with the password you set earlier in this guide.

AUTH <PASSWORD>

If successfully authenticated, you will see the message “OK” appear in the terminal.

OK

13. Once authenticated, the easiest way to verify that the server is storing and retrieving information is to use the “INCR” command on a new variable.

In our example, we are increasing the entry “examplecounter” by 1.

INCR examplecounter

After running this command, you should see that the value has been increased by 1 from the previous run.

(integer) 1

Updating your Redis Docker Container

As mentioned at the start of this guide, one of the benefits of using Docker to run software like Redis is that it makes updating a straightforward process.

These quick few steps will show you how easy the update process is.

1. First, you must ensure you are in the same directory in which you wrote the Docker Compose file.

You need to be in the correct directory so that Docker knows what containers it needs to pull an image for.

cd /opt/stacks/redis

2. After changing to the correct directory, you will want to tell Docker that it should pull the latest version of the Redis container.

All we need to do to get Docker to do this is to use the following command. It will scan the Compose file, find all images, and pull a new version if it’s available.

docker compose pull

3. Even though we downloaded a new image, it doesn’t mean it will automatically move Redis to the latest version of the Docker container.

We will want to run the following command to get Docker to restart the container using the new version of the Redis image.

docker compose up

Conclusion

Hopefully, at this stage, you will now have a good understanding of how to use Redis by using a Docker container.

Running Redis in a Docker container is a great way to get the software up and running quickly. Additionally, if you use it with other containers, you can include it within the same Compose file to make connecting even easier.

Please feel free to leave a comment below if you have had any issues with getting Redis up and running using Docker.

If you found this guide to be helpful, we highly recommend checking out our many other tutorials.

Leave a Reply

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