Last active
February 10, 2021 21:28
-
-
Save flickerfly/0bd2bfeb124eca6f002a0e1c03c12869 to your computer and use it in GitHub Desktop.
This bash loop will wait for a docker container by name (or ID) to get in a running state before it will allow further execution of the script.
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
# Set to name or ID of the container to be watched. | |
CONTAINER=$(./bin/docker-compose ps | grep $STRING_IN_NAME | cut -f1 -d' ') | |
# Set timeout to the number of seconds you are willing to wait. | |
timeout=500 | |
# Leave this alone | |
counter=0 | |
# This first echo is important for keeping the output clean and not overwriting the previous line of output. | |
echo "Waiting for $CONTAINER to be ready (${counter}/${timeout})" | |
#This says that until docker inspect reports the container is in a running state, keep looping. | |
until [[ $(docker inspect --format '{{json .State.Running}}' $CONTAINER) == true ]]; do | |
# If we've reached the timeout period, report that and exit to prevent running an infinite loop. | |
if [[ $timeout -lt $counter ]]; then | |
echo "ERROR: Timed out waiting for $CONTAINER to come up." | |
exit 1 | |
fi | |
# Every 5 seconds update the status | |
if (( $counter % 5 == 0 )); then | |
echo -e "\e[1A\e[KWaiting for $CONTAINER to be ready (${counter}/${timeout})" | |
fi | |
# Wait a second and increment the counter | |
sleep 1s | |
((counter++)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment