Atlantic.Net Blog

How to Deploy and Run Redis in Docker

Redis is an open-source, fast, and in-memory key-value data store. It can be used as a database, cache, message broker, and queue. Redis is designed for distributed cluster environment and it can handle large datasets. Deploying Redis in a Docker container will help to scale Redis and fit it right into your development environment.

In this post, we will show you how to deploy Redis with Docker on Ubuntu 20.04.

Step 1 – Install Docker

By default, the latest version of Docker is not included in the Ubuntu default repository, so you will need to add the Docker official repository to your system.

First, install all required dependencies using the following command:

apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y

Next, add the Docker repository with the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Once the repository is added, install Docker with the following command:

apt-get install docker-ce -y

Once Docker is installed, check the status of Docker with the following command:

systemctl status docker

Output:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-06-07 07:14:36 UTC; 13s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 9081 (dockerd)
      Tasks: 10
     Memory: 41.0M
     CGroup: /system.slice/docker.service
             └─9081 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Step 2 – Download Redis Image

Next, you will need to download the latest version of the Redis image from the Docker registry. You can download it with the following command:

docker pull redis:latest

Output:

latest: Pulling from library/redis
69692152171a: Pull complete 
a4a46f2fd7e0: Pull complete 
bcdf6fddc3bd: Pull complete 
2902e41faefa: Pull complete 
df3e1d63cdb1: Pull complete 
fa57f005a60d: Pull complete 
Digest: sha256:7e2c6181ad5c425443b56c7c73a9cd6df24a122345847d1ea9bb86a5afc76325
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

You can now verify the downloaded image with the following command:

docker images

Output:

REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
redis        latest    fad0ee7e917a   5 days ago   105MB

Step 3 – Launch Redis Container

Next, you will need to launch a Redis container from the downloaded image. You can launch it with the following command:

docker run --name redis-instance -dit redis

Output:

2e7b27647159791d589cbfd55ce3503b8e4cae56b79eaecedeff77cb1824904a

Once the container is started, you can check it with the following command:

docker ps

You should see the following output:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS      NAMES
2e7b27647159   redis     "docker-entrypoint.s…"   11 seconds ago   Up 9 seconds   6379/tcp   redis-instance

As you can see, the Redis container is started and listening on port 6379.

Step 4 – Connect the Redis Container

Next, you can connect to the Redis container with the following command:

docker exec -it redis-instance /bin/bash

Once you are connected, you should see the following shell:

root@2e7b27647159:/data#

Now, connect to the Redis interface with the following command:

root@2e7b27647159:/data# redis-cli

Output:

127.0.0.1:6379> 

Now, test the Redis with the ping command:

127.0.0.1:6379> ping

If everything is fine, you should see the following output:

PONG

Run the quit command to exit from the Redis CLI.

127.0.0.1:6379> quit

Next, exit from the Redis container with the following command:

root@2e7b27647159:/data# exit

Conclusion

Congratulations! You have successfully deployed a Redis within a Docker container. You can now use this setup to deploy and scale a Redis within a Docker cluster on your dedicated hosting account from Atlantic.Net.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year