Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.
# 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.
# Leave this alone
# 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
# Every 5 seconds update the status
if (( $counter % 5 == 0 )); then
echo -e "\e[1A\e[KWaiting for $CONTAINER to be ready (${counter}/${timeout})"
# Wait a second and increment the counter
sleep 1s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment