Last active
September 8, 2018 09:53
-
-
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
This file contains hidden or 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
# 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