Skip to content

Instantly share code, notes, and snippets.

@The-Running-Dev
Created May 16, 2025 18:08
Show Gist options
  • Save The-Running-Dev/20f9a0d595fb422d05fe048e3e82207a to your computer and use it in GitHub Desktop.
Save The-Running-Dev/20f9a0d595fb422d05fe048e3e82207a to your computer and use it in GitHub Desktop.
Watchdog Container
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]
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
#!/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