Redis is an exceptionally fast remote in-memory database solution. The key-value data model enables Redis to handle large datasets while maintaining high availability and read-write speeds.
NoSQL databases, like Redis, are meant to run efficiently in distributed clusters that scale out horizontally. Using Docker to deploy Redis in a container makes horizontal scaling a routine, straightforward process.
This tutorial explains how to deploy Redis in a Docker container in no time.
- Access to a command line
- A working Docker installation
- A user with root or sudo privileges
Note: To deploy a Redis container, the Docker service needs to be active and running. If you need help to install Docker on Ubuntu, Centos, Debian, or Raspberry Pi, use the respective links and see our detailed guides.
Start a Docker Redis Container
1. Check the current status of the Docker service by entering the following command in your terminal:
sudo systemctl status docker
The output confirms that Docker is running and active.
2. Retrieve and start a Redis container (my-first-redis) with the
docker run command:
sudo docker run --name my-first-redis -d redis
The command did not specify a Redis version. The system proceeds to download the latest available version of Redis by default.
3. Once the installation process is complete, check the status of current docker containers with the
docker ps command:
sudo docker ps
Among other information, the system provides:
- The unique container ID – b36262951bf4
- Access Port – 6379 (default Redis port number)
- The defined container name – my-first-redis
Connect to Redis with redis-cli
Start the interactive
redis-cli command shell using the following command:
sudo docker exec -it my-first-redis sh
Note: You can also use the unique container ID (b36263951bf4) instead of the container name.
Once you access the interactive shell, type
redis-cli to connect to the Redis container instance.
Try Basic Redis Commands
1. The Redis
ping command is useful for testing if a connection to the Redis database is active:
The response, PONG, indicates that the connection is successful.
2. Key-value stores use the simplest possible data model. A unique key is paired with a value. Use the set command to define the key
name and the value pair as
set name pnap
3. You can retrieve the value using the unique key
name and the
The result retrieves the previously defined
pnap value. A list of data types and commands is available in our comprehensive guide Redis Data Types With Commands.
4. Once you have explored redis-cli commands, type
quit to return to the container terminal interface.
exit to close the connection with the Docker container.
Using a Custom redis.conf File (Optional)
The redis.conf file allows you to set up authentication, limit commands, and define other security-oriented settings.
If you have created a custom Redis configuration file, use the following command to load the file at container launch:
sudo docker run --name my-first-redis -v /myfirstredis/redis.conf:/usr/local/etc/redis/redis.conf -d redis
The location of the redis.conf file in this example is myfirstredis/redis.conf. Change the path to match the location on your system.
Access Redis from Another Docker Container
1. Use the
--link option to create and connect a new container to the existing Redis instance:
sudo docker run -it --rm --name my-second-redis --link my-first-redis:redis -d redis
The command initiated a new Redis container (my-second-redis), based on the initial redis image. The my-first-redis container is going to be referred to as redis within the second container.
--rm option ensures that the second container deletes itself after exiting the interactive shell. This action is optional and is used to conserve resources.
2. Initiate the interactive shell within the my-second-redis container:
sudo docker exec -it my-second-redis sh
3. Start the Redis command-line in the my-second-redis container and connect to my-first-redis (now named redis), with the following command:
# redis-cli -h redis
4. The original my-first-redis container is still active. The
name key created earlier is available and can be accessed by the second container:
redis:6379> get name
The resulting pair value is
quit to leave the redis-cli and then type
exit to return to your terminal:
redis:6379> quit # exit
Note: Read our guide to learn more about sharing data between Docker containers.
Access Redis from Remote Server
You can use the Docker port-forwarding function to access Redis containers from remote servers.
1. Define the port to be used for the remote connection:
sudo docker run --name my-first-redis -p [port_number]:6379 -d redis
2. Access the Redis container from a remote server using the host-name or IP and the newly defined port number:
sudo redis-cli -h [host or IP] -p [port_number] -a [password]
-a authentication flag is optional. If used, it requests users to enter their password to access the Redis database.
Note: If you are working with Kubernetes, our Knowledge Base also has a guide on how to deploy a Redis Cluster on Kubernetes.
You have successfully deployed a Redis instance within a Docker container.
Use the commands presented in this tutorial to deploy Redis on other servers in your cluster, link the containers, and access Redis containers from remote servers.