docker,

How to monitor and restart unhealthy Docker Containers

Jan 24, 2022 · 1 min read · Post a comment

Last week, I wrote about How to restart a single container with Docker Compose. The approach is manual, hence will be repetitive and error-prone. And since we want to automate almost everything as a DevOps engineer, today I’ll show you a better way of handling unhealthy containers.

Prerequisites

  • Docker
  • Docker Compose

A few days ago, I found this popular Docker image with around 107 millions docker pulls, so I thought to myself, this must be it. Lightweight, simple, straight-to-the-point solution.

From the CLI

docker run -d \
    --name autoheal \
    --restart=always \
    -e AUTOHEAL_CONTAINER_LABEL=all \
    -v /var/run/docker.sock:/var/run/docker.sock \
    willfarrell/autoheal

Docker Compose

version: "3.7"
services:
  autoheal:
    restart: on-failure
    image: willfarrell/autoheal
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - "AUTOHEAL_CONTAINER_LABEL=all"
  ...

Conclusion

Give it a spin and let me know what you think. You could even add a Slack or MS Teams integration, by passing a webhook URL to the WEBHOOK_URL environment variable. Official GitHub repo: https://github.com/willfarrell/docker-autoheal
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.