How to deploy redis on Docker

Deploying and Running Redis on Docker: A Simple Guide

Introduction

Redis, an open-source in-memory data structure store, is widely used for caching, session management, and message queuing in modern applications. Docker, a popular containerization platform, provides a convenient way to package and deploy applications, including Redis, as lightweight, self-contained containers. Combining the power of Redis and Docker provides developers with a flexible and efficient solution for managing data storage and retrieval. In this article, we will guide you through the process of deploying and running Redis on Docker, enabling you to leverage the benefits of containerization and Redis.

Step 1: Installing Docker

Before we dive into the deployment process, ensure that you have Docker installed and running on your system. You can visit the official Docker website (https://www.docker.com/) to download and install Docker based on your operating system.

Before we begin, ensure that Docker is installed on your machine. Docker provides platform-specific installation instructions for various operating systems on their official website (https://www.docker.com). Follow the instructions to install Docker Community Edition (CE) or Docker Desktop, depending on your OS.

On this guidance we have already installed Docker on Ubuntu 22.04 LTS operating system, the guidance can be found at How To Install Docker On Ubuntu 22.04 article. To ensure if Docker is already running, we will verify it by querying its status by submitting command line :

$ sudo systemctl status docker

Output :

ramansah@bckinfo ~]$ sudo systemctl status docker
[sudo] password for ramansah: 
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-11-07 13:40:02 WIB; 2min 31s ago
Docs: https://docs.docker.com
Main PID: 1407 (dockerd)
Tasks: 55
Memory: 159.5M
CGroup: /system.slice/docker.service
├─1407 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
├─2802 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 5555 -container-ip 172.18.0.5 -co>
├─2829 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 5555 -container-ip 172.18.0.5 -contain>
├─2894 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.18.0.8 -co>
└─2920 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8080 -container-ip 172.18.0.8 -contain>

Nov 07 13:40:22 bckinfo dockerd[1407]: time="2021-11-07T13:40:22.283703896+07:00" level=warning msg="Health c>
Nov 07 13:40:22 bckinfo dockerd[1407]: time="2021-11-07T13:40:22.302180675+07:00" level=warning msg="Health c>
Nov 07 13:40:42 bckinfo dockerd[1407]: time="2021-11-07T13:40:42.315818519+07:00" level=warning msg="Health c>
Nov 07 13:40:42 bckinfo dockerd[1407]: time="2021-11-07T13:40:42.349911239+07:00" level=warning msg="Health c>
Nov 07 13:40:56 bckinfo dockerd[1407]: time="2021-11-07T13:40:56.667105865+07:00" level=warning msg="Health c>
Nov 07 13:41:02 bckinfo dockerd[1407]: time="2021-11-07T13:41:02.370967330+07:00" level=warning msg="Health c>
Nov 07 13:41:16 bckinfo dockerd[1407]: time="2021-11-07T13:41:16.682283304+07:00" level=warning msg="Health c>
Nov 07 13:41:22 bckinfo dockerd[1407]: time="2021-11-07T13:41:22.328623186+07:00" level=warning msg="Health c>
Nov 07 13:41:22 bckinfo dockerd[1407]: time="2021-11-07T13:41:22.399528242+07:00" level=warning msg="Health c>
Nov 07 13:41:42 bckinfo dockerd[1407]: time="2021-11-07T13:41:42.412815777+07:00" level=warning msg="Health c

Step 2: Obtaining the Redis Docker Image

Docker Hub is a central repository that hosts a vast collection of Docker images, including the official Redis image. To obtain the Redis image, open a terminal or command prompt and execute the following command:

$ docker pull redis

This command will download the latest Redis image from Docker Hub and prepare it for deployment on your local machine.

Step 3: Running Redis Container

With the Redis image in hand, you can now start a Redis container. Run the following command:

$ docker run --name my-redis-container -d redis

Let’s break down the command:

  • docker run: This command creates and runs a new container.
  • –name my-redis-container: Specifies the name for the Redis container. You can choose any name you like.
  • -d: Runs the container in detached mode, which allows it to run in the background.
  • redis: Refers to the Redis image name.

Once executed, Docker will pull the Redis image (if not already available locally) and create a new container based on that image. The container will start running in the background.

The example output will be shown below :

ramansah@bckinfo ~]$ sudo docker run --name otodigi1-redis -d redis
[sudo] password for ramansah: 
bd6e9fdc492a4d3ec63db976b9bc59993c6e21fb49282b251cc0bf021c433029

Step 4: Interacting with Redis Container

To interact with the Redis container, you can use the redis-cli command-line tool. First, find the container ID or name by executing the following command:

$ docker ps

Example output :

[ramansah@bckinfo ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd6e9fdc492a redis "docker-entrypoint.s…" 24 seconds ago Up 23 seconds 6379/tcp otodigi1-redis
c9d574433311 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 3 weeks ago Up 13 minutes (unhealthy) 8080/tcp ramansah_airflow-triggerer_1
e29e5d0f1cc0 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 3 weeks ago Up 13 minutes (healthy) 0.0.0.0:5555->5555/tcp, :::5555->5555/tcp, 8080/tcp ramansah_flower_1
8b92d740dc4d apache/airflow:2.2.0 "/usr/bin/dumb-init …" 3 weeks ago Up 13 minutes (healthy) 8080/tcp ramansah_airflow-scheduler_1
fe73e92aaf83 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 3 weeks ago Up 13 minutes (healthy) 8080/tcp ramansah_airflow-worker_1
a893a3f83b41 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 3 weeks ago Up 13 minutes (healthy) 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp ramansah_airflow-webserver_1
28a59444a7ae redis:latest "docker-entrypoint.s…" 3 weeks ago Up 13 minutes (healthy) 6379/tcp ramansah_redis_1
c6252b3bc5c5 postgres:13 "docker-entrypoint.s…" 3 weeks ago Up 13 minutes (healthy) 5432/tcp ramansah_postgres_1

Locate the Redis container in the list of running containers and note its ID or name. Then, run the following command to open a Redis command-line interface:

$ docker exec -it <container_id_or_name> redis-cli

Replace <container_id_or_name> with the actual ID or name of your Redis container. Now, you can use Redis commands as if you were interacting with a locally installed Redis server.

The example output will be as shown below :

$ sudo docker exec -it otodigi1-redis sh
ramansah@bckinfo ~]$ sudo docker exec -it otodigi1-redis sh
[sudo] password for ramansah: 
# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name otodigi-start
OK
127.0.0.1:6379> get name
"otodigi-start"

Step 5: Persistence and Configuration

By default, the Redis container runs in memory, which means that any data stored in Redis will be lost when the container is stopped or restarted. To enable persistence, you can mount a volume from your local machine to the container. For example:

$ docker run --name my-redis-container -v /path/on/host:/data -d redis

This command maps the /path/on/host directory on your local machine to the /data directory inside the container. Redis will now write its data to the host machine, allowing it to persist even if the container is restarted.

Additionally, you can customize the Redis configuration by providing your own redis.conf file. Create a redis.conf file on your local machine and mount it to the container using the -v flag, similar to the volume mapping above:

$ docker run --name my-redis-container -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf -d redis redis-server /usr/local/etc/redis/redis.conf

Conclusion

Deploying and running Redis on Docker simplifies the process of setting up and managing Redis instances. With Docker, you can easily create isolated and portable Redis containers, allowing for flexible deployment across different environments. By following the steps outlined in this article, you can harness the power of Redis within a Docker container, taking advantage of its scalability, performance, and ease of management.

 

 

(Visited 145 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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