Answer a question

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
}

Answers

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.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐