Files
projects/Dockers/docker-watcher/watch.sh
T
Bram 0cb1f25dff
Build and Push Docker Images / build-and-push (push) Failing after 1m12s
docker watcher
2026-04-21 20:25:46 +02:00

57 lines
1.5 KiB
Bash

#!/bin/sh
set -eu
WATCH_CONTAINER="${WATCH_CONTAINER:?set WATCH_CONTAINER to the container name or id to watch}"
RESTART_CONTAINERS="${RESTART_CONTAINERS:?set RESTART_CONTAINERS to a comma-separated list of containers to restart}"
POLL_INTERVAL="${POLL_INTERVAL:-30}"
# Only restart when status *becomes* unhealthy (avoids restarting every poll while still unhealthy).
# Set RESTART_ON_EVERY_POLL=1 to restart on every poll while unhealthy.
RESTART_ON_EVERY_POLL="${RESTART_ON_EVERY_POLL:-0}"
log() {
printf '%s %s\n' "$(date '+%Y-%m-%dT%H:%M:%S')" "$*"
}
inspect_health() {
docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$1" 2>/dev/null || printf 'missing'
}
restart_list() (
IFS=','
for c in $RESTART_CONTAINERS; do
c=$(echo "$c" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
[ -z "$c" ] && continue
if docker restart "$c" 2>/dev/null; then
log "restarted: $c"
else
log "restart failed: $c"
fi
done
)
prev=""
log "watching health of: $WATCH_CONTAINER (interval ${POLL_INTERVAL}s)"
log "will restart on unhealthy: $RESTART_CONTAINERS"
while true; do
status="$(inspect_health "$WATCH_CONTAINER")"
case "$status" in
missing)
log "watch target not found: $WATCH_CONTAINER"
;;
unhealthy)
if [ "$RESTART_ON_EVERY_POLL" = "1" ] || [ "$prev" != "unhealthy" ]; then
log "unhealthy — restarting configured containers"
restart_list
fi
;;
*)
;;
esac
prev="$status"
sleep "$POLL_INTERVAL"
done