Introduction
As an in-memory database, Redis offers high availability and throughput when processing large datasets. Docker containerization enables efficient horizontal scaling for Redis deployments, facilitating the management of big data workloads.
This tutorial will explain how to deploy and run Redis in Docker.
Prerequisites
- Command-line access.
- Administrative privileges.
- Docker installed.
Note: If you don't have Docker installed, read how to install Docker on Ubuntu, CentOS/Rocky Linux, Debian, or Raspberry Pi.
Deploy and Run Redis Docker Image
Redis provides an official Docker image on Docker Hub. Follow the steps below to pull and run a Redis container based on this image.
Step 1: Check Docker Status
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.
Press Q to exit the status report.
Step 2: Start Redis Docker Container
Execute the following docker run command to pull the Redis image and use it to start a container:
docker run --name [container_name] -d redis
The command uses the -d
option to start the container in the detached mode. The example below creates a detached container named test-redis:
docker run --name test-redis -d redis
The output confirms the successful download of the image and prints the new container's ID.
Step 3: Check Container Status
List the running docker containers with the docker ps
command:
docker ps
The output shows the new Redis container and the relevant information, including:
- Unique container ID.
- Access port. The default Redis port number is 6379.
- Container name.
Step 4: Connect to Redis with redis-cli
Each Redis container features the Redis command-line interface, which simplifies database management. Access the interface by following the steps below:
1. Start the interactive command shell inside the container using the following command:
docker exec -it [container_name] sh
Note: You can also use the unique container ID instead of the container name.
For example, to execute a shell inside a container named test-redis, type:
docker exec -it test-redis sh
A container shell prompt appears.
2. Type redis-cli
to connect to the Redis container instance.
redis-cli
The Redis CLI prompt shows the IP address and the port of the Redis instance.
Basic Redis Commands
Below is a table listing the most commonly used Redis CLI commands and their function:
Command | Description |
---|---|
SET | Set the key value. |
GET | Retrieve the key value. |
KEYS | Find all keys matching a pattern, e.g., keys test* |
DEL | Delete a key. |
EXISTS | Check if a key exists. |
EXPIRE | Set the key expiration time. |
TTL | Get the remaining time-to-live for a key. |
INCR | Increment the key value by 1. |
DECR | Decrement the key value by 1. |
PING | Check if the server is reachable. The expected output is PONG . |
QUIT | Exit redis-cli. |
Note: View a comprehensive list of data types and commands in our Redis Data Types With Commands guide.
Deploy Redis with Dockerfile
To run a container using a customized Redis image, create the image in Dockerfile using the steps below:
1. Create a Dockerfile in a project directory using a text editor such as Nano:
nano Dockerfile
2. Paste the following code into the file:
FROM redis:latest
EXPOSE 6379
CMD ["redis-server"]
The code consists of the following directives:
FROM
. Determines the base image.EXPOSE
. Exposes a port.CMD
. Provides a command to execute inside the container.
Add further customizations based on the specific needs of the project.
3. Save the file and exit.
4. Build the image using the docker build command in the Dockerfile's directory:
docker build -t [image_name] .
5. Start the container from the custom image by typing the following command:
docker run -itd --name [container_name] -p 6379:6379 [custom_image_name]
Start Redis on Docker Using Custom Local Configuration File
The redis.conf file is the primary configuration file for a Redis server. The user can create a custom Redis configuration file for repeatable customized deployments.
Use the following command to load the file at container launch:
docker run --name test-redis -v [path_to_redis.conf]:/usr/local/etc/redis/redis.conf -d redis
Note: For more information about redis.conf, read the Redis configuration section in the official documentation.
Access Redis from Another Docker Container
Multi-container Docker environments frequently have one or more containers interacting with the Redis server. Follow the steps below to set up access to Redis from another Docker container:
1. Use the --link
option to create and connect a new container to the existing Redis instance:
docker run -it --rm --name [container_name] --link [redis_instance]:redis -d redis
For example, the following command initiates a new Redis container named test-redis-2 and links the test-redis container to the new container:
docker run -it --rm --name test-redis-2 --link test-redis:redis -d redis
The test-redis:redis
segment simplifies referencing of the Redis server (test-redis) by assigning it an alias (redis). The alias is valid within the new container's environment.
The --rm
option ensures that the second container deletes itself after exiting the interactive shell.
2. Initiate the interactive shell within the new container:
docker exec -it [container_name] sh
3. Start the Redis CLI in the new container and connect to the server using the following command:
redis-cli -h redis
The -h
option references the Redis server alias created in the docker run
command above. The alias also appears as part of the new Redis CLI shell prompt.
Note: Read our guide to learn more about sharing data between Docker containers.
Access Redis from Remote Server
Use the Docker port-forwarding function to access Redis containers from remote servers:
1. When creating a server container, define the port for the remote connection:
docker run --name [container_name] -p [port_number]:6379 -d redis
2. Access the Redis server from a remote host using the hostname or IP and the specified port number:
redis-cli -h [host_or_IP] -p [port_number] -a [password]
The -a
authentication flag is optional. If used, it requests users to enter their password to access the Redis database.
How to Configure Redis on Docker
Docker allows Redis users to customize container settings, including ports, storage, and more. The following sections show how to configure Redis within a Docker environment.
Ports
To configure a Redis instance for networking, you can:
- Expose the default Redis port 6379 with the
-p
option:
docker run -d -p 6379:6379 redis
- Expose a custom port on the host system with the
-p
option:
docker run -d -p [custom_port_number]:6379 redis
- Expose both the default and any number of custom ports using the Docker Compose code below:
version: "3.9"
services:
redis:
image: redis:latest
ports:
- "6379:6379"
- "[custom_port_number]:6379"
Persistence
By default, Redis container data is lost once the user stops or removes the container. Use the command below to set up data persistence and permanently store Redis data (RDB and AOF files) inside a Docker volume:
docker run -d -p 6379:6379 -v [volume_name]:[path_in_container] redis
For example, to create a volume named redis-data and connect it to the /data directory in the Redis container, type the following command:
docker run -d -p 6379:6379 -v redis-data:/data redis
For Docker Compose deployments, use the services.redis.volumes section as in the example code below:
version: "3.9"
services:
redis:
image: redis:latest
ports:
- "6379:6379"
volumes:
- redis_data:/data
Note: Read the official documentation to learn more about Redis persistence.
Environment Variables
Use environment variables to pass configuration options into a Redis container. Variables provide a flexible way to customize Redis without modifying the base image.
Use the -e
option to specify an environment variable when executing a docker run
command:
docker run -d -p 6379:6379 -e [env_key]=[env_value] redis
Pass multiple variables by adding multiple -e
options. The example below creates a Redis container and specifies two environment variables:
docker run -d -p 6379:6379 -e REDIS_MAXMEMORY=1GB -e APPENDONLY=yes redis
The following are the variables used in the example above:
REDIS_MAXMEMORY
. Limits the maximum amount of memory Redis can use.APPENDONLY
. Enables the append-only file (AOF) for data persistence.
In Docker Compose deployments, use the following code to achieve the same effect:
version: "3.9"
services:
redis:
image: redis:latest
ports:
- "6379:6379"
environment:
REDIS_MAXMEMORY: 1GB
APPENDONLY: yes
Conclusion
By following this tutorial, you have successfully deployed a Redis instance within a Docker container. The article also showed you how to set up Redis on other servers in your cluster, link the containers, and access Redis containers from remote servers.
If you are working with Kubernetes, our Knowledge Base also has a guide on how to deploy a Redis Cluster on Kubernetes.