How and Why To Run Redis in Docker

Redis is a highly available in-memory database. Its disk-persistent key-value data store is ideal for multiple high-availability uses.

On the other hand, Docker is a tool for containerizing applications and services, isolating their environments and allowing them to run separately. You can use Docker as a package manager for applications and services with all necessary dependencies, configurations, scripts, etc. A script called a Dockerfile is placed inside the Docker container and allows you to deploy the container as a single entity.

This article teaches you how to use these two open-source technologies effectively. It also touches on why running Redis clusters inside Docker containers is vital and the advantages and benefits of doing so compared to running Redis clusters on virtual machines or bare metal servers.

The Various Uses of Redis

Redis has several advantages. Its most popular uses include:

  • Distributed, key-value database cache
  • Message broker
  • Persistent object caching server

Plus, Redis can support multiple data types and structures, making it suitable for use as both a primary and secondary database layer for applications. It also supports lists, streams, hashes, sets, etc.

However, you can push Redis’ benefits further by containerizing it.

Redis is best inside a container! 💯 Learn how to use Redis & Docker effectively and unlock the advantages of containerizing your Redis apps - all in this post ⬇Click to Tweet

Redis Is Best Inside a Container

Redis is one of the most popular Docker images in the Docker registry, with over one billion downloads. The reason for containerizing Redis applications is to get Docker’s benefits and community support for the most common Redis use cases.

The Redis Docker image has an open-source community that creates and maintains clear documentation. And to top it off, Docker is highly reliable and enables easy use and flexibility.

Ease of Use

Docker isolates the environments for both apps and services running inside containers. Isolation means you can package, build, and ship Redis images that work regardless of the host operating system, which makes developing and running Redis applications inside Docker easy. Furthermore, Docker Desktop simplifies setting up a consistent local development environment, which is especially useful for large teams.

Security:

You can scan for security vulnerabilities with Redis inside a container. Docker uses Snyk, a tool that provides visibility into the safety of Docker images. It scans Docker containers and provides details and remediation in case of corrupt images. Docker can also generate a software bill of material (SBOM) for scanning all the dependencies used in building container images, although it’s still experimental.

Flexibility

Docker is available for most Unix-based and Windows operating systems and applications. Since you use containers to isolate software from its environment to ensure it works with all systems, Redis apps, and services always run despite the infrastructure.

Reliability

Docker allows you to run any containerized workload in any environment reliably. Redis is reliable in Docker because Docker containers periodically take snapshots of the container image, allowing you to revert to these snapshots in case of a bad or corrupted database. You can also create a Docker volume by mounting a directory containing the Redis database file inside the container.

A Docker volume helps to keep the container safe from any form of accidental data loss. However, a caveat is that Docker volumes don’t include image snapshots. You must manually save or back up the data in your Redis database file periodically.

How To Use Redis With Docker

To put your Redis store inside a Docker container, you must have Docker in the target environment. The following instructions show you how to install Docker on Windows.

You can follow a guide on the Docker website for a Unix or Unix-based OS. After installation and configuration, you can create a network inside your Docker container. Then you can install Redis and create a database. Finally, you can perform operations on your Redis database.

First, install Docker on your operating system. Optionally, navigate to the Docker Hub and create an account. You need an account to create and publish your images.

Docker hub sign-up form
Docker Hub

After creating an account, sign in to Docker Hub and download your operating system’s latest version of Docker Desktop.

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

After the installation procedure, go to your terminal window to see if the Docker Engine is ready to run and enter the following command:

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 for defining and running multi-container Docker applications using YAML files, which you use to configure application services.

You can run a Dockerized Redis to validate that the Docker Engine is up and running using the code below. This example runs the Redis image 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

Because you don’t have the image locally, Docker pulls the Redis image from the Docker Hub.

Now, you can open Docker Desktop to confirm that it pulled the Redis image. On the Dashboard, click Images on the left-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 uses the bridge or the overlay built-in network drivers to create a Docker network. You can also install and use a third-party network driver.

The difference is that bridge networks are isolated on a single Docker Engine installation, while overlay networks span multiple Docker hosts, each running an engine.

Type:

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

If you don’t specify any drivers by default, Docker uses the bridge driver to create a new network since it automatically creates the bridge driver when you install the Docker Engine. However, if you run a Docker container with the Docker run command, it won’t use this network.

Now that you have a network, installing Redis inside the Docker container is next. To do so, type: docker pull redis.

Pull the Redis image from the Docker Hub
Pull the Redis image from the Docker Hub

To verify the Redis image is installed correctly, run one of the following commands:

docker images
docker image ls
List available Docker images in the user's terminal
Available Docker images in the user’s terminal

Next, you create and start the Redis container. To do so, run the following command:

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

Now verify that Redis is running in 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 in a container

Now that you have confirmed that your Docker container is up and running, you can add the container to the network. Type: docker network connect .

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

With everything working as expected, the next step is to verify your Redis server running inside Docker is ready to accept 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 a database because you need a way to connect to the Redis container to run commands on the server. To do this, type: docker exec -it bash. You can use the container name and container ID interchangeably.

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

In the container, use the CLI to run commands. Note that Redis automatically installed Docker hosts. To use the Redis-CLI, type: redis-cli.

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

This connects you to the default port for Redis on the local host. Type: ping, and the response should be pong.

Test to see if the Redis connection is up and running
Test to see if the Redis connection is up and running

Next, select a database inside your Redis server by typing select. Although the default database is 0, select db 1.

Select a particular Redis database
Select a particular Redis database

Next, insert data into the database. Use the Redis SET command. To do so, type SET <key> <value>.

For example:

SET mykey "Hello"

To get the same key, type:

get myKey
Insert and retrieve data from Redis
Insert and retrieve data from Redis

To get the value for the key, run get. This returns the value. Next, monitor your Redis database by typing MONITOR.

Monitor the Redis database
Monitor the Redis database

Finally, halt the Redis server by exiting from the CLI. Type exit twice to exit the CLI and Redis.

Exit from the Redis CLI
Exit from the Redis CLI

Finally, stop the Redis container from running by typing docker stop.

Execute the command to stop the Docker container from running
Execute the command to stop the Docker container from running

Redis With DevKinsta

DevKinsta lets you connect a WordPress site to a Redis database. With DevKinsta, the Redis add-on works seamlessly as an object cache to persist data across several page loads. This means that by using the Redis add-on, you can use the cache to reuse objects instead of constantly querying the MySQL database.

After downloading and setting up DevKinsta, install the Docker Desktop as a dependency, which it uses to create local environment files inside a container. With this setup, you can configure the Redis add-on in Devkinsta.

Therefore, the Redis add-on for DevKinsta can be used as a secondary source, reducing the load on the primary database, increasing response times, and making the application work faster.

This translates into the app handling more load and scaling efficiently. Check out this post to use the Redis plugin with your website.

Redis + Docker = a match made in heaven! 🤩 Here's why containerizing Redis app is the way to go and how to make the most of it. 👇Click to Tweet

Summary

There are many advantages to running Redis inside a Docker container. It offers a sandboxed environment for your applications, so you no longer have to care about the platform where you deploy your applications. Furthermore, Docker enables ease of use, flexibility, and high reliability.

Regarding security, you could check and scan for security vulnerabilities out of the box with Synk, a tool providing visibility into the safety of Docker images. And regarding flexibility, Docker works for most Unix-based and Windows operating systems and applications. This flexibility means that dockerized or containerized apps or services always run irrespective of the OS.

It’s easy to use Docker and the Redis add-on with DevKinsta, which spins up a dockerized WordPress site locally when you start it. Check out DevKinsta and the Redis add-on to get the power of Redis in Docker today.

The post How and Why To Run Redis in Docker appeared first on Kinsta®.

版权声明:
作者:感冒的梵高
链接:https://www.techfm.club/p/44952.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>