### Docker Compose Deployment with Autoheal Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Deploy autoheal alongside your application stack using Docker Compose. This example demonstrates how to enable autoheal for different services using labels and configure health checks. ```yaml version: '3.8' services: webapp: image: nginx:alpine labels: autoheal: "true" healthcheck: test: ["CMD", "curl", "-f", "http://localhost/"] interval: 30s timeout: 10s retries: 3 start_period: 10s api: image: myapi:latest labels: autoheal: "true" autoheal.stop.timeout: "20" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"] interval: 15s timeout: 5s retries: 3 database: image: postgres:15 labels: autoheal: "true" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 autoheal: image: willfarrell/autoheal:latest restart: always network_mode: none environment: AUTOHEAL_CONTAINER_LABEL: autoheal AUTOHEAL_INTERVAL: 5 AUTOHEAL_START_PERIOD: 60 volumes: - /var/run/docker.sock:/var/run/docker.sock - /etc/localtime:/etc/localtime:ro ``` -------------------------------- ### Webhook Notifications with Docker Run Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Configure webhook notifications to receive alerts when containers are restarted. This example shows how to set up Slack, Discord, and Apprise notifications using `docker run` commands. ```bash # Slack webhook notification docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ -e WEBHOOK_URL="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" \ -e WEBHOOK_JSON_KEY="text" \ -v /var/run/docker.sock:/var/run/docker.sock \ willfarrell/autoheal # Webhook payload format: # {"text": "Container myapp (abc123def456) found to be unhealthy. Successfully restarted the container!"} # Discord webhook (use "content" as JSON key) docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ -e WEBHOOK_URL="https://discord.com/api/webhooks/xxx/yyy" \ -e WEBHOOK_JSON_KEY="content" \ -v /var/run/docker.sock:/var/run/docker.sock \ willfarrell/autoheal # Apprise notification service docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ -e APPRISE_URL="http://apprise-server:8000/notify/all" \ -v /var/run/docker.sock:/var/run/docker.sock \ willfarrell/autoheal # Apprise payload format: # {"title": "Autoheal", "body": "Container myapp (abc123def456) found to be unhealthy. Successfully restarted the container!"} ``` -------------------------------- ### Build and Run Docker Autoheal Locally Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md These commands demonstrate how to build the Docker Autoheal image locally using `docker buildx` and then run it. The run command configures it to monitor all containers by default using the UNIX socket. ```bash docker buildx build -t autoheal . docker run -d \ -e AUTOHEAL_CONTAINER_LABEL=all \ -v /var/run/docker.sock:/var/run/docker.sock \ autoheal ``` -------------------------------- ### Run Basic Tests with Shell Script Source: https://github.com/willfarrell/docker-autoheal/blob/main/tests/README.md This snippet demonstrates how to navigate to the tests directory and execute the main test script. It's used for basic testing of the Docker Autoheal functionality. ```shell cd tests ./tests.sh ``` -------------------------------- ### Building and Testing Autoheal Locally Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Build the autoheal image locally for development and run the test suite to validate functionality. This includes building the image using `docker buildx` and executing the provided test script. ```bash # Build the image locally docker buildx build -t autoheal . # Run local build docker run -d \ --name autoheal \ -e AUTOHEAL_CONTAINER_LABEL=all \ -v /var/run/docker.sock:/var/run/docker.sock \ autoheal # Run the test suite cd tests ./tests.sh # Test suite creates three containers: # 1. should-keep-restarting - Unhealthy container with autoheal label (should restart) # 2. shouldnt-restart-healthy - Healthy container with autoheal label (should NOT restart) # 3. shouldnt-restart-no-label - Unhealthy container without label (should NOT restart) ``` -------------------------------- ### Configure Autoheal with Environment Variables Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Demonstrates running the Docker Autoheal container with various environment variables to customize its behavior, including filtering, intervals, timeouts, and notification settings. ```bash docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=autoheal \ -e AUTOHEAL_INTERVAL=5 \ -e AUTOHEAL_START_PERIOD=30 \ -e AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 \ -e AUTOHEAL_ONLY_MONITOR_RUNNING=false \ -e CURL_TIMEOUT=30 \ -e WEBHOOK_URL="https://hooks.slack.com/services/xxx/yyy/zzz" \ -e WEBHOOK_JSON_KEY="text" \ -e APPRISE_URL="http://apprise:8000/notify" \ -e POST_RESTART_SCRIPT="/scripts/notify.sh" \ -v /var/run/docker.sock:/var/run/docker.sock \ willfarrell/autoheal # Environment Variable Reference: # AUTOHEAL_CONTAINER_LABEL - Label to filter containers (default: "autoheal", use "all" for all containers) # AUTOHEAL_INTERVAL - Health check polling interval in seconds (default: 5) # AUTOHEAL_START_PERIOD - Delay before first health check in seconds (default: 0) # AUTOHEAL_DEFAULT_STOP_TIMEOUT - Seconds to wait for container stop during restart (default: 10) # AUTOHEAL_ONLY_MONITOR_RUNNING - Only monitor running containers, ignore paused (default: false) # CURL_TIMEOUT - Max seconds for Docker API requests (default: 30) # WEBHOOK_URL - URL for restart notifications (default: "") # WEBHOOK_JSON_KEY - JSON key for webhook payload (default: "text") # APPRISE_URL - Apprise notification service URL (default: "") # POST_RESTART_SCRIPT - Script to execute after container restart (default: "") ``` -------------------------------- ### Run CI Tests with Specific Labels and Build Numbers Source: https://github.com/willfarrell/docker-autoheal/blob/main/tests/README.md This snippet shows how to run tests in a Continuous Integration environment. It involves setting an environment variable for a unique container label and passing a unique build number to the test script to ensure targeted restarts. ```shell cd tests export "AUTOHEAL_CONTAINER_LABEL=autoheal-123456" ./tests.sh "MY_UNIQUE_BUILD_NUMBER_123456" ``` -------------------------------- ### Label Containers for Autoheal Monitoring Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Shows how to apply labels to Docker containers to enable or disable autoheal monitoring. Containers must have a HEALTHCHECK defined to be monitored. Custom stop timeouts can also be specified. ```bash # Run a container with autoheal monitoring enabled docker run -d \ --name myapp \ --label autoheal=true \ --health-cmd="curl -f http://localhost:8080/health || exit 1" \ --health-interval=30s \ --health-timeout=10s \ --health-retries=3 \ --health-start-period=40s \ myapp:latest # Optionally set a custom stop timeout for this specific container docker run -d \ --name myapp \ --label autoheal=true \ --label autoheal.stop.timeout=30 \ --health-cmd="curl -f http://localhost:8080/health || exit 1" \ --health-interval=30s \ myapp:latest # Disable autoheal for a specific container (when using AUTOHEAL_CONTAINER_LABEL=all) docker run -d \ --name excluded-app \ --label autoheal=False \ myapp:latest ``` -------------------------------- ### Run Docker Autoheal with UNIX Socket Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md This command demonstrates how to run the Docker Autoheal container using a UNIX socket for communication with the Docker daemon. It maps the Docker socket into the container and sets a label to monitor all containers. ```bash docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ -v /var/run/docker.sock:/var/run/docker.sock \ willfarrell/autoheal ``` -------------------------------- ### Run Docker Autoheal with TCP Socket Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md This command shows how to configure Docker Autoheal to connect to the Docker daemon via a TCP socket. It requires specifying the Docker host and port, and optionally mounting certificates for secure communication. ```bash docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ -e DOCKER_SOCK=tcp://$HOST:$PORT \ -v /path/to/certs/:/certs/:ro \ willfarrell/autoheal ``` -------------------------------- ### Run Docker Autoheal with TCP Socket and mTLS Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md This command illustrates running Docker Autoheal with mutual TLS (mTLS) for secure communication over a TCP socket. It requires mounting CA certificates, client certificates, and client keys, and setting specific environment variables for TLS verification. ```bash docker run -d \ --name autoheal \ --restart=always \ --tlscacert=/certs/ca.pem \ --tlscert=/certs/client-cert.pem \ --tlskey=/certs/client-key.pem \ -e AUTOHEAL_CONTAINER_LABEL=all \ -e DOCKER_HOST=tcp://$HOST:2376 \ -e DOCKER_SOCK=tcps://$HOST:2376 \ -e DOCKER_TLS_VERIFY=1 \ -v /path/to/certs/:/certs/:ro \ willfarrell/autoheal ``` -------------------------------- ### Map Local Timezone to Autoheal Container Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md This command snippet shows how to map the host's local time file (`/etc/localtime`) into the Docker Autoheal container. This ensures that the container's timezone matches the host's timezone. ```bash docker run ... -v /etc/localtime:/etc/localtime:ro ``` -------------------------------- ### Custom Label-Based Filtering with Docker Compose Source: https://context7.com/willfarrell/docker-autoheal/llms.txt Use custom labels to create separate autoheal instances for different container groups. This allows for granular control over which containers are monitored by specific autoheal instances. ```yaml version: '3.8' services: critical-app: image: critical:latest labels: autoheal-critical: "true" healthcheck: test: ["CMD", "curl", "-f", "http://localhost/health"] interval: 10s timeout: 5s retries: 2 background-worker: image: worker:latest labels: autoheal-workers: "true" healthcheck: test: ["CMD", "pgrep", "-f", "worker"] interval: 30s timeout: 10s retries: 3 autoheal-critical: image: willfarrell/autoheal:latest restart: always environment: AUTOHEAL_CONTAINER_LABEL: autoheal-critical AUTOHEAL_INTERVAL: 3 volumes: - /var/run/docker.sock:/var/run/docker.sock autoheal-workers: image: willfarrell/autoheal:latest restart: always environment: AUTOHEAL_CONTAINER_LABEL: autoheal-workers AUTOHEAL_INTERVAL: 15 volumes: - /var/run/docker.sock:/var/run/docker.sock ``` -------------------------------- ### Docker Compose Configuration for Autoheal Source: https://github.com/willfarrell/docker-autoheal/blob/main/README.md This Docker Compose configuration defines a service for Docker Autoheal. It specifies the image, restart policy, volume mounts for the Docker socket and local time, and environment variables to control which containers are monitored. ```yaml services: app: extends: file: ${PWD}/services.yml service: app labels: autoheal-app: true autoheal: deploy: replicas: 1 environment: AUTOHEAL_CONTAINER_LABEL: autoheal-app image: willfarrell/autoheal:latest network_mode: none restart: always volumes: - /etc/localtime:/etc/localtime:ro - /var/run/docker.sock:/var/run/docker.sock ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.