Reputation: 8944
I am writing a shell script in which I am installing docker on Mac through terminal. I am able to install through brew cask install docker
. Now it is GUI based docker which wait for user event to continue. Now in my shell script I want hold the execution until docker is started and running. I have found a nice script which checks if docker is running or not. I am able to check it though as below.
#!/bin/bash
rep=$(curl -s --unix-socket /var/run/docker.sock http://ping > /dev/null)
status=$?
if [ "$status" == "7" ]; then
echo 'not connected'
exit 1
fi
echo 'connected'
exit 0
I have tried to it put in a loop so that until status is 0 it should check again and again and hold the exception. I have no experience in shell scripting but tried a way to run but failed to do . how can I achieve this.
My script
#!/bin/bash
status=0
test() {
rep=$(curl -s --unix-socket /var/run/docker.sock http://ping > /dev/null)
status=$?
}
checkDocker() {
while [ "$status" == "7" ]
do
echo waiting docker to start
test
done
}
Upvotes: 9
Views: 2116
Reputation: 473
Here's what I use. It's a modification of jeffbymes' answer. Instead printing the error messages and waiting for 10 seconds between retries, this just prints a nice message with an ellipsis that grows one dot every second until Docker is ready.
#!/bin/bash
printf "Starting Docker for Mac";
open -a Docker;
while [[ -z "$(! docker stats --no-stream 2> /dev/null)" ]];
do printf ".";
sleep 1
done
echo "";
Upvotes: 10
Reputation: 2221
Lifted from https://stackoverflow.com/a/48843074/133479, here’s the script I came up with while trying to get yours to work. Checking the exit of the curl
is tricky, because while Docker is starting up, it will respond with an error, but since the response occurs, curl
considers it OK and $status
is set to a value other than 7
.
#!/bin/bash
while (! docker stats --no-stream ); do
# Docker takes a few seconds to initialize
echo "Waiting for Docker to launch..."
sleep 10
done
docker start $container
One thing to consider, however, is that this eventually has output, so you may wish to handle that.
Upvotes: 2