Docker Autoheal

repository·main·Indexed 24 days ago

https://github.com/willfarrell/docker-autoheal

A utility container that monitors Docker containers for unhealthy status via HEALTHCHECK and automatically restarts them. It supports connection via UNIX socket, TCP socket, or TCP with mTLS, and can be configured to monitor containers using specific labels or by watching all containers.

Tokens
1.6K
Snippets
5
Records
6
Agent score
34%

What's inside docker-autoheal

  1. Configure Docker Autoheal target containers

    main

    Docker Autoheal identifies which containers to monitor using labels. You can configure this in three ways:

    1. Specific Label: Apply the label autoheal=true to your target container.
    2. Watch All: Set the environment variable AUTOHEAL_CONTAINER_LABEL=all in the Autoheal container to monitor every container.
    3. Custom Label Key: Set AUTOHEAL_CONTAINER_LABEL to the name of an existing label that has the value true (e.g., if your containers have my-label: true, set AUTOHEAL_CONTAINER_LABEL=my-label).
  2. Run Docker Autoheal via Docker CLI

    main

    You can run Docker Autoheal as a standalone container to monitor and restart unhealthy containers. Depending on your Docker setup, you can connect via a UNIX socket, a TCP socket, or TCP with mTLS (HTTPS).

    Important: You must have HEALTHCHECK defined in your target container images for Autoheal to function.

    #### UNIX socket passthrough
    ```bash
    docker run -d \
        --name autoheal \
        --restart=always \
        -e AUTOHEAL_CONTAINER_LABEL=all \
        -v /var/run/docker.sock:/var/run/docker.sock \
        willfarrell/autoheal

    TCP socket

    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

    TCP with mTLS (HTTPS)

    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
  3. Configure Docker Autoheal with Docker Compose

    main

    This example demonstrates how to set up Autoheal in a docker-compose.yml file, targeting containers that have a specific label (autoheal-app: true) and mapping the local timezone and Docker socket.

    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
  4. Reference: Docker Autoheal Environment Variables

    main

    Use these environment variables to configure the behavior of the Autoheal container.

    |Variable                              |Description|
    |--------------------------------------|------------|
    |`AUTOHEAL_CONTAINER_LABEL=autoheal`   |set to existing label name that has the value `true`|
    |`AUTOHEAL_INTERVAL=5`                 |check every 5 seconds|
    |`AUTOHEAL_START_PERIOD=0`             |wait 0 seconds before first health check|
    |`AUTOHEAL_DEFAULT_STOP_TIMEOUT=10`   |Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)|
    |`AUTOHEAL_ONLY_MONITOR_RUNNING=false` |All containers monitored by default. Set this to true to only monitor running containers. This will result in Paused contaners being ignored.|
    |`DOCKER_SOCK=/var/run/docker.sock`    |Unix socket for curl requests to Docker API|
    |`CURL_TIMEOUT=30`                     |--max-time seconds for curl requests to Docker API|
    |`WEBHOOK_URL=""`                     |post message to the webhook if a container was restarted (or restart failed)|