Created
May 16, 2025 18:08
-
-
Save The-Running-Dev/20f9a0d595fb422d05fe048e3e82207a to your computer and use it in GitHub Desktop.
Watchdog Container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x-commonKeys: &commonOptions | |
| restart: always | |
| stdin_open: true | |
| tty: true | |
| x-dnsServers: &dnsServers | |
| dns: | |
| - 45.90.28.29 | |
| - 45.90.30.29 | |
| services: | |
| watchdog: | |
| image: alpine | |
| container_name: watchdog | |
| volumes: | |
| - /var/run/docker.sock:/var/run/docker.sock | |
| - ./watchdog.sh:/watchdog.sh | |
| entrypoint: /bin/sh | |
| command: /watchdog.sh | |
| <<: [*dnsServers, *commonOptions] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| something: | |
| container_name: something | |
| image: lscr.io/linuxserver/prowlarr:latest | |
| .... | |
| healthcheck: | |
| test: curl -sf http://localhost || exit 1 | |
| interval: 60s | |
| timeout: 30s | |
| retries: 5 | |
| start_period: 30s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| apk add --no-cache curl bash docker || exit 1 | |
| echo "๐ Watchdog Started. Monitoring Containers..." | |
| while true; do | |
| unhealthy=$(docker ps --format '{{.Names}}' | while read name; do | |
| health=$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{end}}' "$name") | |
| if [ -z "$health" ]; then | |
| # Container does not have health check | |
| return | |
| fi | |
| if [ "$health" = "starting" ]; then | |
| # Container is still starting | |
| return | |
| fi | |
| # Container is unhealthy | |
| if [ "$health" = "unhealthy" ]; then | |
| echo "$name" | |
| fi | |
| done) | |
| for c in $unhealthy; do | |
| echo "[$(date)] ๐ Restarting Unhealthy Container: $c" | |
| docker restart "$c" | |
| done | |
| sleep 60 | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment