Skip to content

Instantly share code, notes, and snippets.

@arnested
Last active September 8, 2018 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnested/fbcbab69c91b13960641eebb13ff6e4c to your computer and use it in GitHub Desktop.
Save arnested/fbcbab69c91b13960641eebb13ff6e4c to your computer and use it in GitHub Desktop.
docker-compose shell function wrapper to wait for all containers to become healthy
# Uses `alerter` from https://github.com/vjeantet/alerter
# Uses `healthy` from https://github.com/arnested/go-healthy
# Uses `mapfile` bash builtin introduced in bash4
docker-compose () {
show_success_message () {
message="'${COMPOSER_PROJECT_NAME:-$(basename "$(git root)")}' is healthy."
# Run in sub shell to get rid of info about background job.
(nohup alerter -message "${message}" -title 'Docker Compose' -appIcon /Applications/Docker.app/Contents/Resources/AppIcon.icns -sound Glass < /dev/null &> /dev/null &)
}
command docker-compose "$@"
exitcode=$?
if [ "$exitcode" != "0" ]; then
return $exitcode
fi
# Only wait if for containers to become healthy if docker-compose
# command is `up` and we run in an interactive shell.
if [ "$1" = "up" ] && [[ $- =~ i ]]; then
mapfile -t container_ids < <(command docker-compose ps -q)
echo -n "Waiting for containers to become healthy... "
healthy "${container_ids[@]}" && show_success_message && echo "done"
fi
return $exitcode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment