Adding Redis easily with Docker

Overview

In this chapter, i'll walk you through the installation, configuration and how to use Redis in a simple way. In this process, i'll use Docker to create a container with the Redis image.

Requirements

  • Docker

Installation & Configuration

In this first step, you need to install Docker on your machine by following the steps below.

1.Update Software Repositories:

sudo apt-get update

2.Install some prerequisite packages that let apt use packages over HTTPS:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

3.So, add GPG Key to repository Docker on your SO:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4.Add the repository Docker to fonts of the APT:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

5.Again:

sudo apt update

6.Finally, install Docker:

sudo apt-get install docker-ce docker-ce-cli containerd.io

Check if the Docker was installed correctly:

sudo docker run hello-world

Executing the command Docker without sudo:

sudo usermod -aG docker ${USER}

Working with images from Docker

Docker containers are built with Docker images. By default, it searches from Docker Hub, where the image is registered.

docker run --name redis-tutorial -p 6379:6379 -d redis

Explanation of this command. For the creation of a container, we need an image, if there is no image already created, the run command will search the internet and download it to your host (which is your local machine) and automatically create a container. The --name command allows you to name the container. The -p command maps port 6379 on the container (right side) to port 6379 on the host (left side). The -d command runs the container in the background. The last command is the name of the image it should fetch from the remote repository redis.

Okay, if all went well, you should find your container. To do this is very simple! The docker ps command lists all containers that are active, and you should have found something like this:

CONTAINER ID| IMAGE| COMMAND| CREATED| STATUS| PORTS| NAMES
763fa8c3e7ca| redis| "docker-entrypoint.s…"| 2 hours ago| Up 2 hours| 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp| redis-tutorial

In the first column is the Container ID, in the second column is image (redis, postgres, ruby...), in the third the command it runs in the background, in the fourth and fifth, the date the container was created and respectively the status in which he finds himself. Right at the end, we have the port and the connection protocol that was made and lastly you must remember, it is the name that we defined in the previous command.

Well, now that we have a container running in the background and we know its name, we need to go in, after all, the container is a separate process from the operating system and if we want to run things in there, we need to tell the docker that we want it, right ?
The exec command executes something inside the container and there are several modes and types of commands for this, but for now, let's just go into the container! Run:

docker exec -it redis-tutorial bash

explanation of this command line: exec allows you to execute commands in the container without the need to be inside it. -it is a way to associate your terminal and interact with the container. bash will cause us to enter the container's BASH.

Now that we are inside the container with a Redis image, we can run the Redis CLI:

redis-cli.

Try Redis commands, for example: hmset student:xxx id 1 college nnn address xn. If the return is OK, it means everything is fine and you can now play with Redis :)

20