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.conf2. 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 /dataMake 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:
4. Running the Redis Container
Navigate to the redis-setup directory and start the Redis container:
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:
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
5.2. Testing the Connection with redis-cli
To test the connection from a command line:
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:
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