How To Run Redis in Podman / Docker Container

Posted on 150 views

Redis an acronym for REmote DIctionary Server is an open-source, in-memory key-value pair NoSQL database written in ANSI C. It is a data structure store that can be used as a primary database, message broker, session store, or as a cache to web and gaming applications. This in-memory database is optimized for speed with both high read and write speeds since all the data in Redis is stored in the RAM. It also supports graphs, search, analytics, real-time streaming, and many more features than that of a simple data store.

To give maximum CPU optimization, Redis is designed to use the single-threaded event loop model. Data structures used internally are as well implemented for maximum performance. Other features associated with Redis are:

  • High availability and scalability – witht the primary-replica architecture, you can build highly available solutions providing consistent performance and reliability. It can be scaled vertically and horizontally
  • Data Persistence – Saved data lasts even if the server failure occurs. For data persistent, redis must write on permanent storage such as hard disk.
  • Rich Data Structures – It offers an innumerable variety of data structures to meet the desired application needs.
  • Simplicity – it simple in design with very fewer number of lines to be integrated to be able to store, access, and use data.
  • In-memory datastore – in contrast to conventional relational databases such as SQL, Oracle, e.t.c that store most data on disks, Redis and other in-memory datastores do not suffer the same penalty to access to access disks, this in turn gives applications super-fast performance and support for innumerable operations per second.

Redis can be deployed on clouds, on-premises, hybrid environments, and over the Edge devices. This guide offers an in-depth illustration of how to run Redis in Podman / Docker Container.

Step 1 – Install Podman|Docker on your system

We will begin by installing Podman|Docker on our system. Install the desired container engine on your system.

Install Docker using the aid from the below guide.

For Podman, proceed using the commands below.

#On CentOS/Rocky Linux
sudo yum install podman

#On Debian
sudo apt-get install podman

#On Ubuntu
. /etc/os-release
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_$VERSION_ID/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_$VERSION_ID/Release.key" | sudo apt-key add -
sudo apt update
sudo apt -y install podman

#On Fedora
sudo dnf install podman

#On RHEL 7
sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
sudo yum -y install podman

#On RHEL 8
sudo yum module enable -y container-tools:rhel8
sudo yum module install -y container-tools:rhel8

Verify the installation as below.

$ podman info
host:
  arch: amd64
  buildahVersion: 1.23.1
  cgroupControllers: []
  cgroupManager: cgroupfs
  cgroupVersion: v1
  conmon:
    package: conmon-2.0.29-1.module+el8.4.0+643+525e162a.x86_64
    path: /usr/bin/conmon
    version: 'conmon version 2.0.29, commit: ce0221c919d8326c218a7d4d355d11848e8dd21f'
  cpus: 2
  distribution:
    distribution: '"rocky"'
    version: "8.4"
  eventLogger: file
  hostname: localhost.localdomain
  idMappings:
    gidmap:
    - container_id: 0
      host_id: 1000
      size: 1
    - container_id: 1
      host_id: 100000
      size: 65536
    uidmap:
    - container_id: 0
      host_id: 1000
      size: 1
    - container_id: 1
      host_id: 100000
      size: 65536
.....

For Debian/Ubuntu systems, you may be required to make the below configurations to work with OCI registries.

$ sudo vim /etc/containers/registries.conf
unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"]

Once the desired container engine has been installed, proceed to the below step.

Step 2 – Create a Persistent Volume for the Redis Container

Persistent volumes here help data to survive after the main process of the particular data has ended. To achieve this, we need to create volumes on the hard disk to store the data.

sudo mkdir -p  /var/redis/data
sudo mkdir $PWD/redis-data
sudo chmod 775 -R /var/redis/data
sudo chmod 775 -R $PWD/redis-data

On Rhel-based systems, you are required to set SELinux in permissive mode otherwise, the created path will be inaccessible.

sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config

Step 3 – Provision the Redis Container

First, pull the Redis container image.

##For Podman
podman pull docker.io/redis

##For Docker
docker pull docker.io/redis

Sample output:

Using default tag: latest
latest: Pulling from library/redis
5eb5b503b376: Pull complete 
6530a7ea3479: Pull complete 
91f5202c6d9b: Pull complete 
9f1ac212e389: Pull complete 
82c311187b72: Pull complete 
da84aa65ce64: Pull complete 
Digest: sha256:0d9c9aed1eb385336db0bc9b976b6b49774aee3d2b9c2788a0d0d9e239986cb3
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

Once pulled, verify if the image exists on your local registry.

##For Podman
$ podman images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
redis        latest    f1b6973564e9   3 weeks ago   113MB

##For Docker
$ docker images
REPOSITORY               TAG         IMAGE ID      CREATED      SIZE
docker.io/library/redis  latest      f1b6973564e9  3 weeks ago  116 MB

Step 4 – Run the Redis Container

With the image available in the local registry, we can now spin the Redis container with Podman\Docker or with Podman-Compose|Docker-compose

1. Using Podman|Docker

  • Using Podman
podman run -d \
  --name redis_server \
  -v $PWD/redis-data:/var/redis/data  \
  -p 6379:6379 \
  redis --requirepass StrongPassword
  • Using Docker
docker run -d \
  --name redis_server \
  -v $PWD/redis-data:/var/redis/data  \
  -p 6379:6379 \
  docker.io/library/redis --requirepass StrongPassword

2. Using Podman-Compose|Docker-compose

You can as well use Podman-Compose|Docker-compose to spin the container. All you need is to have Podman-Compose|Docker-compose installed.

Install Podman-compose using the commands:

First, install Python and PIP.

# Install Python3 on CentOS 7
sudo yum -y install epel-release
sudo yum -y install python3 python3-pip python3-devel

# Install Python3 on Rocky Linux 8 / CentOS Stream 8 / AlmaLinux 8
sudo yum -y install python3 python3-pip python3-devel

# Install Python3 on Debian / Ubuntu
sudo apt update
sudo apt install python3 python3-pip

Now install dotenv and podman-compose as below.

sudo pip3 install python-dotenv
sudo curl -o /usr/local/bin/podman-compose https://raw.githubusercontent.com/containers/podman-compose/devel/podman_compose.py
sudo chmod +x /usr/local/bin/podman-compose

Install docker-compose with the commands:

curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
chmod +x docker-compose-linux-x86_64
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

Now create the YAML file to be used when running the container.

vim docker-compose.yml

In the file, add the lines below.

version: '3'
services:
  cache:
    image: redis
    container_name: redis_server
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --requirepass StrongPassword
    volumes: 
      - $PWD/redis-data:/var/redis/data
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf

In the file above, the –requirepass command has been used to specify a password for our Redis.

Now start the container using the command:

##For Podman
podman-compose up -d

##For Docker
docker-compose up -d

With any other the above methods used, the container will start and can be checked using the command:

##For Podman
$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS                   NAMES
cee0b9192ccb  docker.io/library/redis:latest  --requirepass Str...  7 seconds ago  Up 8 seconds ago  0.0.0.0:6379->6379/tcp  redis_server

##For Docker
$ docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
90775de4796b   redis     "docker-entrypoint.s…"   32 seconds ago   Up 30 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis_server

To start/stop the container, issue the command:

##For Podman
podman stop redis_server
podman start redis_server

##For Docker
docker stop redis_server
docker start redis_server

Step 5 – Run the Redis Container as a systemd service.

The container can be managed like any other systems service. We will create a systems service file for the container as below.

sudo vim /etc/systemd/system/redis-container.service

In the file, add the content below replacing the name of your Container engine. For example for docker:

[Unit]
Description=Redis container

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a redis_server
ExecStop=/usr/bin/docker stop -t 2 redis_server

[Install]
WantedBy=local.target

With Podman, you can also generate the service file and copy it to /etc/systemd/system/redis-container.service as below

podman generate systemd redis_server

Copy the generated file to to /etc/systemd/system/redis-container.service and proceed as below.

Reload the system daemon.

sudo systemctl daemon-reload

Now start and enable the service.

sudo systemctl start redis-container.service
sudo systemctl enable redis-container.service

Once started, check the status as below.

$ systemctl status redis-container.service
 redis-container.service - Redis container
     Loaded: loaded (/etc/systemd/system/redis-container.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-02-20 05:15:00 EST; 8s ago
   Main PID: 5880 (docker)
      Tasks: 7 (limit: 7075)
     Memory: 18.5M
        CPU: 29ms
     CGroup: /system.slice/redis-container.service
             └─5880 /usr/bin/docker start -a redis_server

In case you find any errors such as “restarted too quickly” when starting the Redis container, it is because of permissions and you can correct this by running the Redis container with sudo or with elevated privileges as root

Step 6 – Connect to the Redis container

You can now connect to the Redis container locally or remotely using redis-cli.

Locally, you will access the container as below:

##For docker
docker exec -it redis_server redis-cli

##For Podman
podman exec -it redis_server redis-cli

Sample Output:

How-To-Run-Redis-in-Podman-Docker-Container

Remotely, you need to have redis-tools installed and proceed as below.

sudo redis-cli -h [host IP or domain name] -p 6379

For example for this guide, the command will be:

sudo redis-cli -h 192.168.205.4 -p 6379

Provide the password for the Redis server.

How-To-Run-Redis-in-Podman-Docker-Container-1

Voila!

That was enough learning! I hope this guide has been of great importance to you. You can as give any feedback pertaining to this guide in the comments below.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.