Last active
October 16, 2019 16:09
-
-
Save bastman/d8533163f6fcb5c4ab3ef0df53933f47 to your computer and use it in GitHub Desktop.
docker-compose-wait: wait for docker containers to be ready
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
// https://github.com/ufoscout/docker-compose-wait | |
#!/bin/bash | |
set -e | |
timeout=${WAIT_HOSTS_TIMEOUT:-30} | |
waitAfterHosts=${WAIT_AFTER_HOSTS:-0} | |
waitBeforeHosts=${WAIT_BEFORE_HOSTS:-0} | |
echo "Waiting for ${waitBeforeHosts} seconds." | |
sleep $waitBeforeHosts | |
# our target format is a comma separated list where each item is "host:ip" | |
if [ -n "$WAIT_HOSTS" ]; then | |
uris=$(echo $WAIT_HOSTS | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq) | |
fi | |
# wait for each target | |
if [ -z "$uris" ]; | |
then echo "No wait targets found." >&2; | |
else | |
for uri in $uris | |
do | |
host=$(echo $uri | cut -d: -f1) | |
port=$(echo $uri | cut -d: -f2) | |
[ -n "${host}" ] | |
[ -n "${port}" ] | |
echo "Waiting for ${uri}." | |
seconds=0 | |
while [ "$seconds" -lt "$timeout" ] && ! nc -z -w1 $host $port | |
do | |
echo -n . | |
seconds=$((seconds+1)) | |
sleep 1 | |
done | |
if [ "$seconds" -lt "$timeout" ]; then | |
echo "${uri} is up!" | |
else | |
echo " ERROR: unable to connect to ${uri}" >&2 | |
exit 1 | |
fi | |
done | |
echo "All hosts are up" | |
fi | |
echo "Waiting for ${waitAfterHosts} seconds." | |
sleep $waitAfterHosts | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work! Has made my life so much easier. I will make sure to give you attribution in my code. Cheers!