Created
November 15, 2022 10:30
-
-
Save emilioSp/adc088ac7bdcae9dea1ae18e45ddf3c8 to your computer and use it in GitHub Desktop.
Wait docker container for being healthy
This file contains 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
#!/usr/bin/env bash | |
container_name=$1 | |
shift | |
timeout=$1 | |
default_timeout=120 | |
if [ -z ${timeout} ]; then | |
timeout=${default_timeout} | |
fi | |
RETURN_HEALTHY=0 | |
RETURN_UNKNOWN=3 | |
RETURN_ERROR=99 | |
function usage() { | |
echo " | |
Usage: wait-for-healthy-container.sh <container name> [timeout] | |
" | |
return | |
} | |
function get_health_state { | |
state=$(docker inspect -f '{{ .State.Health.Status }}' ${container_name}) | |
return_code=$? | |
if [ ! ${return_code} -eq 0 ]; then | |
exit ${RETURN_ERROR} | |
fi | |
if [[ "${state}" == "healthy" ]]; then | |
return 0 | |
else | |
return ${RETURN_UNKNOWN} | |
fi | |
} | |
function wait_for() { | |
echo "Wait for container '$container_name' to be healthy for max $timeout seconds..." | |
for i in `seq ${timeout}`; do | |
get_health_state | |
state=$? | |
if [ ${state} -eq 0 ]; then | |
echo "Container is healthy after ${i} seconds." | |
exit 0 | |
fi | |
sleep 1 | |
done | |
echo "Timeout exceeded. Health status returned: $(docker inspect -f '{{ .State.Health.Status }}' ${container_name})" | |
exit 1 | |
} | |
if [ -z ${container_name} ]; then | |
usage | |
exit 1 | |
else | |
wait_for | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment