What are the reasons and How To Run Redis in Docker - (r)

May 1, 2023
Run Redis in Docker

Share this on

This guide will show users how to make use of these two open-source technologies efficiently. It also touches on the reasons why the use of Redis clusters within Docker containers is vital and the advantages and benefits that come from doing this compared to running Redis clusters on the virtual machine or on bare metal servers.

The various ways to use Redis

  • Distributed, key-value database cache
  • Message broker
  • Persistent cache for objects

Additionally, Redis can support multiple kinds of data and structure which makes it suitable for usage as primary as well as a secondary database layer for applications. It also supports lists, streams, hashes, sets, etc.

But, you could extend Redis' advantages further packaging Redis' benefits further by containerizing.

Redis is best inside A Container

Redis is one of the most well-known Docker images in the Docker registry, with more than 1 billion downloaded. The purpose behind the containerization of Redis applications is to get the benefits of Docker and to get community support for the most common Redis usage scenarios.

Redis Docker is a popular image. Redis Docker image has an open-source community, which develops and maintains clear documentation. In addition, Docker is highly reliable and provides easy-to-use and a variety of options.

Ease of Use

Security:

Flexibility

Docker is available to the majority of Unix-based as well as Windows operating systems and applications. Because you utilize containers to shield applications from their environment, you can ensure it works with any system, Redis apps, and applications will always function despite infrastructure.

Reliability

A Docker volume helps to protect the container against any accidental loss of data. But, one caveat to note can be found in that Docker volumes don't include image snapshots. You must manually save or backup the information in your Redis database files regularly.

How To Use Redis With Docker

In order to put your Redis store into an Docker container, you must have Docker within the environment you want to use. These instructions will show you how to set up Docker in Windows.

There is a tutorial on the Docker site for an Unix or an OS based on Unix. After installation and configuration it is possible to create networks inside your Docker container. After that, you'll be able to download Redis and build an account. Then, you are able to perform actions in your Redis database.

First, first, install Docker on your system. If you prefer, visit Docker Hub. Docker Hub and create an account. It is necessary to have an account in order for creating and publishing your images.

Docker hub sign-up form
Docker Hub

After creating an account, log into Docker Hub and download your operating system's latest version. Docker Desktop.

Download page for Docker Desktop with options for operating systems.
Docker download page

After installation After the installation process is completed, open the terminal window and check if it is ready to run. Docker Engine is now ready to run and enter into the command line:

docker --version
Check the version of Docker installed
Docker version installed

Then type: docker-compose --version.

Check the version of Compose installed with Docker
Compose installed with Docker

Compose is a tool to define and run multi-container Docker applications by using YAML files, which you use to configure application services.

It is possible to run Dockerized Redis to confirm that the Docker Engine is running and running using the code below. This test executes the Redis image that is available from Docker Hub and names it my-redis-container.

docker run --name my-redis-container -d redis
Run the Redis image from Docker Hub named my-redis-container
Redis image from Docker Hub named my-redis-container

Since you don't have the Image locally Docker draws it's Redis image from the Docker Hub.

Now, you can start Docker Desktop to confirm that it has pulled it's Redis image. In the Dashboard you can click Images on the left-hand side panel.

Verify that your Redis image is up and running from Docker Desktop
Docker Desktop

Now, you can create a network. Note that the Docker driver makes use of the bridge and the overlay built-in network drivers in order to make a Docker-based network. It is also possible to install and use an external network driver.

The difference is that bridge networks operate from a single Docker Engine installation, while overlay networks span multiple Docker hosts that run an engine.

Type:

docker network create -d bridge -demo-network
Create a new Docker network with the bridge driver
Docker network with the bridge driver

If you do not specify any driver in default, Docker uses the bridge driver to build a brand new network because it automatically makes the bridge driver after installing Docker Engine. Docker Engine. But, if you launch an Docker container using an Docker start command, the container will not use the bridge driver.

Once you've got a network, installing Redis within your Docker container is next. To do so, type: docker pull redis.

Pull the Redis image from the Docker Hub
Remove off the Redis image from the Docker Hub. Docker Hub

To verify the Redis image is properly installed make sure you run any of the following commands:

Docker images
docker image ls
List available Docker images in the user's terminal
Docker images that are available on the terminal of the user


The next step is to create and start by launching the Redis container. For this, you must run this command:

docker run -it -name dev__redis_container -d redis
Create and run the Docker container
Docker conatainer

Then, verify that Redis is working within the container. To do so, type: docker ps.

Verify that Redis is up and running in a container
Verify that Redis is up and running inside a container

Once you've confirmed that your Docker container is up and up and running, you can connect the Docker container onto the network. Type: docker network connect.

Add a Docker container to the created docker network
Connect a Docker container the docker network you created

If everything is working properly Next step is to verify your Redis server running inside Docker is able to handle connections. To do so, use docker logs.

Verify the Redis image is running inside the Docker container
Verify the Redis image is running inside the Docker container

Next, you must create an account in order to have an interface with the Redis container in order to execute commands from the server. To do this, type: docker exec -it bash. The container's name and the ID interchangeably.

Connect to the Redis container instance
Connect to the Redis container.

Inside the container, utilize the CLI in order to execute commands. Be aware that Redis is automatically installed on Docker hosts. To access the RedisCLI enter: redis-cli.

Use the Redis CLI to run Redis commands
Use the Redis CLI to execute Redis commands

This will connect with the port that is used by default for Redis locally on the host. Type: ping, and the response is expected to be "pong".

Test to see if the Redis connection is up and running
Check to determine if it is the case that Redis connection is active and running

Select one of the databases within your Redis server using the word "select". Although the default database is 0 then select the database 1.

Select a particular Redis database
Select a particular Redis database

After that, add data to the database. Utilize to use the Redis set command. To do so, type SET  .

Examples:

SET mykey "Hello"

To obtain the exact key, enter:

Get myKey
Insert and retrieve data from Redis
Insert and retrieve data from Redis

To find the value of the key, you must run find. It returns the key's value. Next, check your Redis database by typing MONITOR.

Monitor the Redis database
Monitor the Redis database

Stop and stop the Redis server by exiting the CLI. Use the command exit repeatedly to quit the CLI as well as Redis.

Exit from the Redis CLI
Exit the Redis CLI

Then, stop the Redis container operating by entering "docker stop".

Execute the command to stop the Docker container from running
Make sure you stop the Docker container from running.

Redis with Dev

After downloading and setting up Dev Install it. Then, install Docker Desktop as a dependency that it utilizes to generate local environment files within a container. Once you have this set-up it is possible to configure the Redis plugin within Dev.

Thus, Redis, the Redis extension to Dev is able to be utilized as a secondary source, reducing the load on the main database, enhancing the speed of response, making the application more efficient.

This results in the application being able to handle greater load and efficiently scaling. Read this article to learn how you can use Redis to integrate the Redis plugin with your website.

Summary

There are numerous benefits when using Redis inside an Docker container. It offers a sandboxed environment for your applications which means you do not have to care about the platform where you deploy your apps. Furthermore, Docker enables ease of using, freedom of use as well as high security.

Concerning security, you may check and scan for security vulnerabilities out of the box using Synk which is a program that provides an overview of the security features in Docker images. And regarding flexibility, Docker works for most Unix-based and Windows operating systems as well as applications. The flexibility of Docker means that containers or dockerized applications or services are always available regardless of the operating system.

  • Easy setup and management in My dashboard. My dashboard
  • Support is available 24/7.
  • The top Google Cloud Platform hardware and network driven by Kubernetes for the highest scalability
  • An enterprise-level Cloudflare integration to speed up as well as security
  • The reach of the global audience is boosted by the possibility of 35 data centers, and around 275 POPs