Docker

How to Install and Run Redis in a Docker Container?

Redis is a free-to-use NoSQL database used by developers around the world. It can be utilized as a database, message broker, and cache. It supports various data structures including hashes, lists, strings, and sets. It is an excellent choice for building web applications, and mobile apps. Developers can install and run Redis in a Docker container as containers provide isolation, efficiency, and portability.

This article will describe the method to install and run Redis in a Docker container.

How to Install and Run Redis in a Docker Container?

To install and run the Redis in a Docker container, try out the below-mentioned steps:

Step 1: Pull Redis Image From Docker Hub
First, download the official Redis image from Docker Hub through the given command:

docker pull redis

According to the above image, the Redis image has been downloaded successfully with the latest version.

Step 2: Run Redis in a Container
Next, type out the provided command to create and run the Redis container:

docker run -d --name redis-cont -p 6379:6379 redis

Here:

  • -d” is utilized to run the Redis container in the background.
  • –name” assigns a name to the container i.e., “redis-cont”.
  • -p” allocates the port for the container i.e., “6379:6379”.
  • redis” is the official Docker image to use for the container:

This command has built and started the Redis container.

Step 3: View Running Redis Container
Then, ensure that the Redis container has been built and running successfully using the given-provided command:

docker ps

It can be observed that the Redis container is running successfully i.e., “redis-cont”.

Step 4: Access Redis Container
After that, execute the “docker exec -it” command and specify the container name to open the Bash shell inside the running Redis container:

docker exec -it redis-cont bash

We have successfully accessed that Redis container and can run commands inside it.

Step 5: Connect to Redis CLI
Now, connect to the Redis CLI by utilizing the below-listed command:

redis-cli

The Redis CLI has been started and now runs the Redis commands in it.

Step 6: Run Redis Commands
Finally, execute some Redis commands for testing. For instance, we have executed the following commands:

ping
set name Laiba
get name

Here:

  • The “ping” command is utilized to test the connectivity between the Redis server and the client.
  • The “set” command sets a key-value pair.
  • The “get” command is used to access the value related to a particular key.

We have successfully installed and run Redis in a container and executed its commands.

Conclusion

To install and run Redis in a Docker container, first, pull the Redis image from Docker Hub. Then, run Redis in a container using the “docker run -d –name <container-name> -p 6379:6379 redis” command and view it. Next, access Redis Container and connect to Redis CLI via the “redis-cli” command. Finally, execute the Redis commands in it. This article has illustrated the procedure to install and run Redis in a Docker container.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.