Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in ssh shell script check for docker version

Tags:

shell

docker

ssh

I am sshing into a server in a shell script and running some docker commands. However some of the commands fail in docker version 1.7. I have a fix for 1.7, but it takes a lot longer to run the shell script if I use it all the time. So I want to check for that version, and if it's 1.7 fall back to the slower script. Else run the nice fast version.

Thinking something like

if ssh -l root $1 "docker -v === '1.7.*'"
then
    echo "Docker version 1.7!"
else 
    echo "Docker version not 1.7!"
fi

Obviously this doesn't work. Any ideas?

like image 578
user3455992 Avatar asked Sep 15 '25 05:09

user3455992


2 Answers

Perhaps, sometime required version range for check. As variant possible solution:

SERVER_VERSION=$(docker version -f "{{.Server.Version}}")
SERVER_VERSION_MAJOR=$(echo "$SERVER_VERSION"| cut -d'.' -f 1)
SERVER_VERSION_MINOR=$(echo "$SERVER_VERSION"| cut -d'.' -f 2)
SERVER_VERSION_BUILD=$(echo "$SERVER_VERSION"| cut -d'.' -f 3)

if [ "${SERVER_VERSION_MAJOR}" -ge 19 ] && \
   [ "${SERVER_VERSION_MINOR}" -ge 0 ]  && \
   [ "${SERVER_VERSION_BUILD}" -ge 3 ]; then
    echo "Docker version >= 19.0.3 it's ok"
else
    echo "Docker version less than 19.0.3 can't continue"
    exit 1
fi
like image 77
Vanya Usalko Avatar answered Sep 17 '25 20:09

Vanya Usalko


You can use:

if [[ $(ssh -l root "$1" 'docker -v') == *" 1.7."* ]]; then
    echo 'Docker version 1.7!'
else 
    echo 'Docker version not 1.7!'
fi
  • You need to take output of whole ssh command using command substitution $(...)
  • Shell supports == or = but not ===
  • Keep glob characters outside the quote
like image 35
anubhava Avatar answered Sep 17 '25 18:09

anubhava