How to Run and Deploy Redis in Docker
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.
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 dockerto 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.
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:
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:
Status: Downloaded newer image for redis:latest
If you want to return a list of all your Docker containers, use the
docker ps command in the terminal:
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:
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:
127.0.0.1:6379> set docker great
127.0.0.1:6379> get docker
127.0.0.1:6379> set name objectrocket
127.0.0.1:6379> get name
Once you’re in the Redis CLI, it can be you can play around with the interface and try out some basic commands.
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:
linux@linux-NECq:~$ docker run -it --rm --name new-redis-container --link container-redis:redis -d redis
We would then execute same
docker command we used before:
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:
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:
redis:6379> get docker
redis:6379> get name
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