Install Redis on Docker Compose
To set up a production-ready Redis instance using Docker Compose with password protection, here is a full setup guide that includes the docker-compose.yml
file, redis.conf
configuration, and connection steps for your application.
1. Folder Structure
Start by creating a directory for your Redis setup, which will include the docker-compose.yml
and redis.conf
files.
redis-setup/
├── docker-compose.yml
└── redis.conf
2. Redis Configuration (`redis.conf`)
Create a redis.conf
file inside the redis-setup
directory. This file will set up Redis with a password and other production settings.
Here is a sample redis.conf
:
# redis.conf
# Require clients to authenticate with a password
requirepass your_secure_password
# Save data every 15 minutes if at least 1 key has changed
save 900 1
# Log level and verbosity
loglevel notice
# Persistent storage file
dbfilename dump.rdb
dir /data
# Disable the protected mode (for internal network use only)
protected-mode no
# Append-only file for durability
appendonly yes
appendfilename "appendonly.aof"
Make sure to replace your_secure_password
with a strong, unique password.
3. Docker Compose File (`docker-compose.yml`)
In the same directory, create a docker-compose.yml
file with the following content:
version: '3.8'
services:
redis:
image: redis:7-alpine
container_name: redis_server
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- redis_data:/data # Persistent storage
- ./redis.conf:/usr/local/etc/redis/redis.conf # Custom configuration
ports:
- "6379:6379"
networks:
- redis_network
restart: unless-stopped
volumes:
redis_data:
networks:
redis_network:
driver: bridge
4. Running the Redis Container
Navigate to the redis-setup
directory and start the Redis container:
docker-compose up -d
This command will start Redis with the configuration specified in redis.conf
, including password protection.
5. Connecting to Redis
With the setup above, Redis will now require authentication. Here’s how to connect securely.
5.1. Connection URL Format
The connection URL for Redis with a password is as follows:
redis://:your_secure_password@<Docker_Host_IP>:6379
Replace <Docker_Host_IP>
with the IP address of your Docker host if accessed externally, or use localhost
if connecting from the same machine.
docker exec -it redis_server redis-cli -a your_secure_password
127.0.0.1:6379>PING
PONG
127.0.0.1:6379> SET testkey "Hello Redis"
OK
127.0.0.1:6379> GET testkey
"Hello Redis"
5.2. Testing the Connection with redis-cli
To test the connection from a command line:
redis-cli -h <Docker_Host_IP> -p 6379 -a your_secure_password
You should replace <Docker_Host_IP>
with the actual IP address if you're connecting externally.
5.3. Application Connection Example (Python)
In your application, use the connection string to connect to the Redis server. Here’s an example using Python with the redis-py
library:
import redis
# Connect to Redis
redis_client = redis.StrictRedis(
host='<Docker_Host_IP>', # Replace with Docker host IP or 'localhost' if local
port=6379,
password='your_secure_password', # Use the password set in redis.conf
decode_responses=True
)
# Test the connection
redis_client.set("test_key", "Hello, Redis!")
print(redis_client.get("test_key")) # Should output: "Hello, Redis!"
6. Persistent Data Storage
The volume redis_data
defined in docker-compose.yml
will ensure that Redis data persists even if the container is restarted. This way, data is retained across restarts, making it suitable for production use.
This setup provides a secure, production-ready Redis environment with persistent storage, password protection, and the ability to connect from an application. Let me know if you need further customization or have any questions!
Last updated