Skip to content

Instantly share code, notes, and snippets.

@jonas-merkle
Created October 31, 2024 16:46
Show Gist options
  • Select an option

  • Save jonas-merkle/17aa9dd49900d2c961011e550789d714 to your computer and use it in GitHub Desktop.

Select an option

Save jonas-merkle/17aa9dd49900d2c961011e550789d714 to your computer and use it in GitHub Desktop.
Docker Compose configuration for setting up a Redis container with a health check
# Docker Compose configuration for setting up a Redis container with a health check
#
# This configuration pulls the latest Redis image and sets up a health check
# to monitor if the Redis service is running properly. The health check uses
# the 'redis-cli ping' command, which returns 'PONG' if Redis is healthy.
#
# Optional configurations include volume mounting for persistence, logging setup,
# and security options.
services:
redis:
image: redis:latest # Pull the latest version of the Redis image
container_name: redis_container # Name of the Redis container
hostname: redis # Hostname for the Redis container
restart: unless-stopped # Restart the container unless manually stopped
# Logging configuration for Redis container
logging:
driver: "json-file" # Use the default json-file logging driver
options:
max-size: "100m" # Maximum log file size before rotation (100 MB)
max-file: "10" # Maximum number of log files to retain (10)
# Security options for Redis container
security_opt:
- no-new-privileges:true # Prevent container processes from gaining additional privileges
# Uncomment and define networks if needed for the service
# networks:
# - application-net
# Environment variables for Redis configuration (if needed)
environment:
# REDIS_PASSWORD: ${REDIS_PW:?Redis password required!} # Uncomment to set a Redis password (for secure deployment)
# Volume configuration for data persistence
volumes:
- ./data/redis:/data # Mount the Redis data directory for persistent storage
# Health check configuration to verify Redis readiness
healthcheck:
test:
[
"CMD-SHELL", # Use the shell form to execute the health check command
"redis-cli ping | grep PONG" # Check if Redis responds with PONG
]
interval: 5s # Time between health check attempts
timeout: 5s # Time to wait for a response from the health check command
retries: 5 # Number of retries before marking the container as unhealthy
# Container labels for additional metadata
labels:
- "com.centurylinklabs.watchtower.enable=true" # Enable automatic updates with Watchtower (optional)
# Uncomment and define networks if needed for the service
# networks:
# application-net:
# driver: bridge # Example of a network configuration
# This health check ensures that the Redis container is only marked as healthy when the
# 'redis-cli ping' command successfully returns "PONG" within the specified timeout.
# If it fails after 5 attempts, the container is marked as unhealthy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment