Redis

How to Run Redis in Docker

Redis is one of the most popular in-memory databases. It is a free and open-source solution that can be used as a caching mechanism or message broker.

Docker is a containerization platform that allows you to develop, package, and ship applications in standalone environments. It enables developers to create application environments that run anywhere with all the tools and required dependencies.

We will learn how to run Redis in a container using Docker for this one.

Requirements

In this tutorial, we assume you have the latest version of Docker installed on your system. If not, you can check the resources below to learn how to accomplish this.

Once you have Docker installed and configured on your system, we can proceed with the tutorial.

Step 1 – Start the Docker Service

The first step is to enable and start the docker service. If you use Docker desktop on Windows, Linux or macOS, run the application to start the service.

You can also use the command as shown below:

sudo systemctl start docker

To check the status of the Docker service, enter the command:

sudo systemctl status docker

Step 2 – Get Docker image

The next step is to pull the docker image from the official docker hub. We can do this by running the command:

sudo docker pull redis

Step 3 – Start Redis Container

To start the Docker container, run the command shown below:

sudo docker run --name redis-server -d redis

The command above should run the docker container with the Redis server.

You can check the status of the docker image using the ps command as:

sudo docker ps

The command above should return an example output as shown:

Test if Redis Server is Up

To test if the Redis server is up and running, open the terminal and run the command:

sudo docker exec -it redis-server sh

The command above gives you a shell session to your docker container.

Run the command above to test the connection to the Redis server

$ redis-cli ping

PONG

If you get the output as PONG, the server is up.

You can then connect to the Redis CLI using the command:

# redis-cli

127.0.0.1:6379>

Step 4 – Stop Docker Container

Once you perform your database operations, you can stop the redis container using the command:

$ sudo docker stop redis-server

Conclusion

This short article discusses building and a Redis server instance on Docker. We hope you enjoyed the tutorial.

Check other linuxhint tutorials to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list