blog
  • Blogs
    • Medium Articles
      • Linux
        • 40 Powerful Linux Networking Commands You Must Know.
        • These (Linux) VI Editor Shortcuts You Must Know
        • Bash/Linux Interview Questions for DevOps Engineers
        • Page 1
      • Git
        • 40 Powerful Git Commands Every Developer Should Know
        • 10 Git Best Practices That Every Developer Must Know
      • DevOps/SRE Interview Questions and Answers
        • Top DevOps/SRE Interview Questions and Answers on AWS VPC
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Terraform Best Practices
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Kubernetes Best Practices
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Dockerfiles
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Grafana
      • Installation
        • Docker Installation on Ubuntu 20/22
        • Install WireGuard VPN on Docker Compose
        • Install Redis on Docker Compose
        • Gravitee Docker Compose
      • Kubernetes Series 2025
        • Understanding Kubernetes: Part 1 -Control Plane
        • Understanding Kubernetes: Part 2 -Worker Node
        • Understanding Kubernetes: Part 3 -Pod
        • Understanding Kubernetes: Part 4-ReplicaSets
        • Understanding Kubernetes: Part 5 -Deployment
        • Understanding Kubernetes: Part 6 -DaemonSets
        • Understanding Kubernetes: Part 7 -StatefulSet
        • Understanding Kubernetes: Part 8 -ConfigMap
        • Understanding Kubernetes: Part 9 -Kubernetes Secret
        • Understanding Kubernetes: Part 10 -StorageClass
        • Understanding Kubernetes: Part 11 -Persistent Volume (PV)
        • Understanding Kubernetes: Part 12 -Persistent Volume Claim (PVC)
        • Understanding Kubernetes: Part 13 -Services
        • Understanding Kubernetes: Part 14 -ClusterIP Service
        • Understanding Kubernetes: Part 15 -NodePort Service
        • Understanding Kubernetes: Part 16 -Load Balancer Service
        • Understanding Kubernetes: Part 17 -Ingress
        • Understanding Kubernetes: Part 18 -Ingress Controller
        • Understanding Kubernetes: Part 19 -Headless Service
        • Understanding Kubernetes: Part 20-Network Policy
        • Understanding Kubernetes: Part 21 -CNI
        • Understanding Kubernetes: Part 22 Kubernetes Resource Requests & Limits
        • Understanding Kubernetes: Part 23 Node Selector
        • Understanding Kubernetes: Part 24 Taints and Tolerations
        • Understanding Kubernetes: Part 25 Affinity and Anti-Affinity
        • Understanding Kubernetes: Part 26 Preemption and Priority
        • Understanding Kubernetes: Part 27 Role and RoleBinding
        • Understanding Kubernetes: Part 28 ClusterRole and ClusterRoleBinding
        • Understanding Kubernetes: Part 29 Service Account
        • Understanding Kubernetes: Part 30 Horizontal Pod Autoscaler (HPA)
        • Understanding Kubernetes: Part 31 Vertical Pod Autoscaler (VPA)
        • Understanding Kubernetes: Part 33 Startup Probe
        • Understanding Kubernetes: Part 34 Liveness Probe
        • Understanding Kubernetes: Part 35 Readiness Probe
        • Understanding Kubernetes: Part 36 Container Network Interface (CNI)
        • Understanding Kubernetes: Part 37 Container Runtime Interface (CRI)
        • Understanding Kubernetes: Part 38 Container Storage Interface (CSI)
      • Cloudflare
        • Cloudflare Tunnel for Secure HTTP Routing
      • Nginx
        • Nginx use cases that every engineer must know
Powered by GitBook
On this page
  1. Blogs
  2. Medium Articles
  3. Installation

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!

PreviousInstall WireGuard VPN on Docker ComposeNextGravitee Docker Compose

Last updated 5 months ago