How to Run and Deploy Redis in Docker

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Redis is a key-value data store that allows users to store and retrieve data at lightning-fast speeds. This zippy performance makes Redis an ideal choice to run in a Docker container. In this article, we’ll show you how simple it is to use Redis and Docker together. We’ll explain each step that’s needed to run and deploy Redis in a Docker container.

Prerequisites

Before we dive further into this tutorial, let’s look at a few prerequisites that are necessary to use Redis and Docker together:

  • First, you’ll need to have Docker installed on your machine. If you’re running Linux, you can use the command sudo systemctl status docker to check Docker’s status. Otherwise, you can download the interactive installer for Docker if you’re using Mac OS X or Windows 10.

  • You’ll need to have some basic knowledge of Docker commands in order to follow along with this tutorial, and you should know how to use a command prompt or terminal.

Redis + Docker

Our first step will be to confirm that Docker is running properly. We’ll use the docker version command to do this, but you can also use the docker info command to return more detailed information about the containers and images that you’re running.

If you’re running Windows, you’ll need to use command prompt to interact with Docker; otherwise, use the terminal if you’re using a UNIX-based operating system like Linux or macOS.

Screenshot of the Docker version for Redis and Docker

Next, let’s use the docker run command to pull and run the Redis Docker image as a container.

Create a Redis container

We can create an instance of Redis running in a container by pulling the image in the Docker.

Shown below is the command syntax we’ll use in Docker to create the Redis database container:

1
docker run --name container-redis -d redis

Redis Docker image

This command should download the latest official Docker image for Redis. The output of the command will look like the following:

1
2
Status: Downloaded newer image for redis:latest
b9e9cdfb78593fa219b1ed736024362d048cf7621846dadc812aa0cd888dcc8f

If you want to return a list of all your Docker containers, use the docker ps command in the terminal:

1
2
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b9e9cdfb7859        redis               "docker-entrypoint.s…"   27 minutes ago      Up 27 minutes       6379/tcp            container-redis

Now, let’s run Redis and execute the redis-cli command inside the container. This will start the interactive shell command-line interpreter. Here’s how to do it:

1
docker exec -it container-redis sh

NOTE: You can also refer to the container by its alphanumeric ID instead of the container-redis container name.

After that, type the command redis-cli to enter into the Redis command-line interface. The following commands can be used to get started:

1
2
3
4
5
6
7
8
127.0.0.1:6379> set docker great
OK
127.0.0.1:6379> get docker
"great"
127.0.0.1:6379> set name objectrocket
OK
127.0.0.1:6379> get name
"objectrocket"

Once you’re in the Redis CLI, it can be you can play around with the interface and try out some basic commands.

Screenshot of Redis Docker container docker run and docker exec

When you’re done with the Redis CLI, type quit to exit and return to the terminal interface for the container. Once you’re outside of Redis, you can type exit to close the interactive connection with the Docker container.

Connect from another container

We can also connect a Redis instance to another Docker container. We just need to create the container with a --link option, which allows us to specify the name of the other container:

1
2
linux@linux-NECq:~$ docker run -it --rm --name new-redis-container --link container-redis:redis -d redis
1b5765e83cd24a7b2038dc4fadeb46aba95d5a38a7e76fcadfef68f323ef55f9

We would then execute same docker command we used before:

1
docker exec -it new-redis-container sh

After running the docker command, we would use the redis-cli command to enter the command-line interface:

1
redis-cli -h redis

Let’s see if everything works. We’ll open a new terminal that will serve as the new container; meanwhile, we’ll observe the other terminal and see if it returns the same value that we created on the first Redis Docker container.

The commands we’ll execute will look like this:

1
2
3
4
redis:6379> get docker
"great"
redis:6379> get name
"objectrocket"

Conclusion

Redis and Docker act as a powerful pair when used together; fortunately, it’s easy to run Redis in a Docker container. In this article, we provided step-by-step instructions for running and deploying Redis in a container. With our examples to guide you, you’ll be able to set up Redis in your own Docker environment.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.