Skip to content

Instantly share code, notes, and snippets.

@arnested
Created September 4, 2018 11:11
Show Gist options
  • Save arnested/55caac1600aad743934f7a67e7ef89d9 to your computer and use it in GitHub Desktop.
Save arnested/55caac1600aad743934f7a67e7ef89d9 to your computer and use it in GitHub Desktop.
Wait for containers to be healthy on `docker-compose up`
#!/bin/bash
interval=10
if [ "$1" = "wait" ]; then
until "$0"
do
echo "Not ready yet. Waiting $interval seconds..."
sleep $interval
done
exit
fi
function finish {
if [ "$status" -eq 0 ]; then
message="Docker for '${COMPOSER_PROJECT_NAME:-$(basename $(git root))}' is ready."
printf '\e]9;%s \007' "${message}"
echo "${message}"
fi
}
trap finish exit
status=0
for id in $(docker-compose ps -q); do
running_status=$(docker inspect --format='{{.State.Running}}' "$id" 2> /dev/null)
if [ "$running_status" != "true" ]; then
continue
fi
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$id" 2> /dev/null)
has_health=$?
if [ $has_health -ne 0 ]; then
continue
fi
case "$health_status" in
starting)
status=1
;;
unhealthy)
status=1
;;
esac
done
exit $status
#!/bin/bash
/usr/local/bin/docker-compose "$@"
exitcode=$?
if [ "$exitcode" != "0" ]; then
exit $exitcode
fi
if [ "$1" = "up" ]; then
dchealth wait
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment