#!/bin/bash # wait-healthy.sh — poll docker health every 10 s until it reads # healthy; exit 1 at the deadline, or at once if the container exits or starts restarting. # "(unhealthy)" alone does not fail it: a seat whose boot outlasts its healthcheck # start_period reads unhealthy and then recovers. Prints the elapsed time either way. set -u c=$1 max=$2 t=0 while [ "$t" -lt "$max" ]; do st=$(docker ps -a --filter "name=^${c}$" --format '{{.Status}}') case "$st" in *"(healthy)"*) echo "$c healthy after ${t}s"; exit 0 ;; Exited*|Restarting*|Dead*) echo "$c FAILED after ${t}s: $st"; exit 1 ;; esac sleep 10; t=$((t + 10)) done echo "$c NOT healthy after ${max}s: $st"; exit 1