How to Deploy and Run Redis in Docker

deploy-run-redis-docker

Deployment of Redis in Docker simplifies managing dependencies, improves efficiency, enhances the output of systems, and decreases conflicts. Redis is essential to streamline DevOps and microservices’ agility. With Docker, the risk of dependency conflicts and the need for rapid resource-intensive tasks are reduced. The multi-tenancy feature of Redis on Docker increases its capability to fight, with higher scalability as well as better isolation. 

In this article, we’ll provide step-by-step directions for deploying as well as running Redis Docker container.

Why Use Docker for Redis?

Let’s look at the key features of Redis and find out how Docker can improve the user experience:

  • Isolation: The containers are isolated from each other.
  • Portability: Redis containers can be used on any system that has Docker installed.
  • Simplicity: Redis images can be taken without having to go through an extensive setup procedure.
  • Scalability: Creating more Redis instances is simple.

How to Deploy and Run Redis in Docker

In this tutorial, we install Redis and test its operations and persistence capabilities, including systems and operations. Let’s begin by installing Redis on Docker:

Prerequisites

Make sure that Docker is installed on your Linux system. To confirm the installation, use the following command:

docker --version
verify docker installation

Step 1: Pulling the Redis Docker Image

For the first time to get started, we’ll pull our Redis Docker image from Docker Hub. Redis offers an official version of its image available on Docker Hub. This means you can download the most recent, safe copies of images on Docker Hub:

docker pull redis:latest

The above command always retrieves the most current image:

pull redis image

Step 2: Running Redis with Basic Configuration

After you have downloaded an image of the file, you are given the option of launching Redis within the Docker container that has the minimum of configurations:

docker run --name [Redis_image] -d redis:latest
run redis with basic config

It creates a new container known as “my-redis” with the Redis image. The -d flag allows the container to be run as detached. This means that it will run in the background. The Redis server is running in the container using port 6379.

Step 3: Exposing Redis Port for External Access

Before you can establish an external connection to your network, ensure you have the Redis port turned on:

docker run --name [Redis_image] -p 6379:6379 -d redis:latest
Exposing Redis Port for External Access

The -p 6379:6379 option connects port 6379 of your host computer to port 6379 within the container. This means that applications running on the host system, in other containers, or even other apps will have the ability to connect to Redis through localhost:6379.

You can confirm it by using this command:

docker ps
confirm container is running

Step 4: Connecting to Redis and Testing

To verify that Redis is necessary to join the Redis server using the Redis CLI. Start by opening the terminal:

docker exec -it [Redis_image] redis-cli
Connecting to Redis and Testing

This command allows you to connect to the container using the Redis CLI.

To determine if Redis is running, try the following commands and verify that Redis is functioning:

SET mykey "Hello World"
GET mykey
verify Redis is functioning

The examples above illustrate the fundamental but essential functions provided by Redis. It allows data to be stored and then retrieved by keys.

Step 5: Implementation of Data Persistence

In our case, with Redis, memory data, or in-memory database data, is crucial. For instance, if the container is stopped, this data is lost. For example:

docker run --name [Redis_image] -p 6379:6379 -v [redis_volume]:/data -d redis:latest redis-server --appendonly yes
implementation of data persistence

The above command creates a named volume redis-data that is mounted to /data in the container. The --appendonly yes flag allows Redis persistent mode using append-only files, where writing operations will be logged for data preservation.

In Docker containers, information is often temporary. If the container is removed, there is a risk of permanent data loss because all stored information and settings will be deleted. In such situations, one must always attach a volume that will allow the user to save the configuration files with the written information:

docker volume inspect [redis_volume]
inspect redis-data

Step 6: Custom Redis Configuration

For production setups, you will likely require custom configurations for Redis. For that, create a Redis configuration file in your local directory:

# Create a custom redis.conf file
echo "maxmemory 256mb
maxmemory-policy allkeys-lru
save 60 1000" > redis.conf

Now, you can run the following command to mount this configuration file into the container:

docker run --name [Redis_image] -p 6379:6379 -v $(pwd)/redis.conf:/usr/local/etc/redis/redis.conf -v [redis_volume]:/data -d redis:latest redis-server /usr/local/etc/redis/redis.conf
mount the configuration file into the container

With this configuration, you can adjust the memory limits, policy for eviction, and other intervals customized to your application’s needs.

Step 7: Securing Redis with Authentication

Production Redis deployments are vulnerable to multiple security risks; therefore, security is essential. You can implement authentication by adding a password to your Redis configuration:

echo "requirepass mypassword" >> redis.conf

With this setup, clients must authenticate before command execution. This can be done by:

docker exec -it [Redis_image] redis-cli
Securing Redis with Authentication

Note: “auth mypassword” verifies authentication by adding a password.

Step 8: Using Docker Compose for Complex Deployments

In case you have advanced scenarios that include several containers or services, Docker Compose is a more appropriate option. You just need to create a docker-compose.yml file:

version: '3.8'
services:
  redis:
    image: redis:latest
    container_name: my-redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    restart: unless-stopped
volumes:
  redis-data:
Use Docker Compose for Complex Deployments

Deploy using the following command:

sudo docker-compose up -d
deploy using docker compose

This way, you have better structure and control over multi-container applications.

Step 9: Monitoring and Logging

For production environments, monitoring Redis performance and Redis logs can be done through:

docker logs [Redis_image]
monitor logs

For more persistent monitoring, run the following commands:

docker logs -f [Redis_image]
persistent monitoring

Monitoring Redis performance can also be done through the CLI by executing the INFO commands, which provide adequate statistics.

Step 10: Cleaning Up Docker Containers and Volumes

While using Docker, you may gather unused containers and volumes. Let’s stop a container:

docker stop [Redis_image]
stop docker container

Now, remove a container:

docker rm [Redis_image]
remove container

Step 11: Maintenance and Updates

Performance monitoring, data backups, and image updates are examples of regular maintenance. To update Redis:

docker pull redis:latest
update Redis

Named volumes ensure that data persists through container recreations.

Final Thoughts

The deployment of Redis is simple in any context with Docker. These steps help in securely deploying Redis while making it easy to scale. Running Redis in Docker is an important skill for developers building systems locally and for DevOps engineers overseeing infrastructure. It improves workflow efficiency as well as the ability to scale services and ensures constant control of resources. When you run multiple containers, a simpler management process and a cleaner environment come. If Redis dockerfile is exposed to the internet, serious security risks can arise. Restrictions via firewalls are essential.

Deploy Docker on UltaHost’s DDoS-protected VPS Hosting for better performance, reliability, and to streamline your workflows. Enjoy ultra-fast SSD NVMe storage, DDoS protection up to 3500+ Gbps, and lightning-fast server deployment at a competitive price. Safeguard your business or online service with our top-tier firewalls!

FAQ

What differences exist in Redis persistence modes?
How do I back up Redis data running in Docker?
Is it possible to run an additional Redis container on the same host?
In what ways can the memory allocation for Redis in Docker be constrained? 
What happens to my Redis data if the container ceases operation?
In what way can I access Redis from another Docker container?
Can Redis be deployed in a production environment using Docker?

Related Post

How to Share Data Between Docker Containers

Docker is a containerization platform that enables us t...

How to Install Jira on Linux

Jira is a popular project management tool developed by ...

How to Set up Nginx as Reverse Proxy for Dock...

Nginx is one of the most popular webserver and revers...

How to Install Plesk on Linux

Plesk is a comprehensive web hosting control panel desi...

How to Install Spark on Ubuntu

Apache Spark offers a powerful open-source framework sp...

How to Install Python on Ubuntu

As a favorite among novice and intermediate developers,...

Leave a Comment

  Enjoy Powerful Next-Gen VPS Hosting from as low as $4.80