
Redis® is an in-memory data store designed for low-latency access, supporting strings, lists, sets, sorted sets, streams, and pub/sub messaging. It is commonly used as a cache layer, message broker, and lightweight in-memory key-value database for high-throughput applications.
This article explains how to deploy Redis® with Docker Compose, enable data persistence, and secure it with a password.
Before you begin, you need to:
sudo privileges.In this section, you create a project directory for Redis® and define environment variables in a .env file that Docker Compose loads automatically.
Create the project directory.
Navigate into the project directory.
Create a .env file to store the environment variables.
Add the following variables:
Replace:
STRONG_REDIS_PASSWORD with a secure alphanumeric password.REDIS_PORT with your preferred port (default: 6379).Save and close the file.
Create a Redis® configuration file.
Add the following Redis settings:
Save and close the file.
In this section, you configure the host system to support reliable Redis® operations. Redis® requires memory overcommit to be enabled. Without this setting, background save operations or replication may fail, and the container may exit unexpectedly.
Open the /etc/sysctl.conf file.
Add the following line to enable memory overcommit.
Save and close the file.
Apply the updated kernel parameters.
Output:
This output confirms that the memory overcommit setting is active on your system.
In this section, you create the Docker Compose manifest that deploys Redis® with data persistence, loads your custom configuration file, and secures the server using a password defined in your .env file.
Create the Docker Compose manifest.
Add the following content:
Save and close the file.
In this manifest:
--requirepass directive.6379) to the host using the value from your .env file../data persists Redis® data across container restarts../conf/redis.conf injects your custom Redis® configuration file into the container.PING every 10 seconds.Start the Redis® service.
Verify that Redis® is running.
Ensure that the output shows the Redis® container is healthy and running.
View logs if needed.
For more information on managing a Docker Compose stack, see the How To Use Docker Compose article.
In this section, you verify that your Redis® instance is reachable and accepting authenticated commands.
Install the Redis® CLI tool on the host.
Test the connection by sending an authenticated PING command.
Replace:
REDIS_HOST with the IP address of your server.REDIS_PORT with the Redis® service port you configured.REDIS_PASSWORD with the password you set for authentication.Output:
This confirms that Redis® is running, reachable, and correctly secured with your password.
You successfully deployed Redis® using Docker Compose with data persistence and password authentication. You configured the required environment variables, applied the necessary host settings, and launched Redis® with a custom configuration file. After verifying the container’s health and testing connectivity, the Redis® instance was fully operational and ready to use.
0 Comments
Be the first to comment and share your perspective with the community.