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:
- Pull the official Redis image through Docker Hub.
- Run Redis in a container using the “docker run -d –name <container-name> -p 6379:6379 redis” command.
- View Running Redis Container.
- Access Redis Container.
- Connect to Redis CLI.
- Run Redis commands.
Step 1: Pull Redis Image From Docker Hub
First, download the official Redis image from Docker Hub through the given command:
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:
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:
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:
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:
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:
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.