8

Is it possible to check via shell script if docker is installed.

I would check this manually by

sudo docker run --name hello-world-container hello-world
sudo docker rm hello-world-container

But I would like to check this programmatically, so if this test fails, the install routine could be started.

Update

#!/bin/bash

which docker

if [ $? -eq 0 ]
then
    docker --version | grep "Docker version"
    if [ $? -eq 0 ]
    then
        echo "docker existing"
    else
        echo "install docker"
    fi
else
    echo "install docker" >&2
fi

1 Answer 1

6

You could try executing:

which docker

and testing the exit code of that command. If it's 0 (successful), you could then execute:

docker --version

and grep for a known name and version pattern.

6
  • Isn't it enough to check which docker? why do I have to check for docker --version after this? And how can I test the exit code? Feb 10, 2017 at 7:21
  • You could satisfy yourself with the exit code from the which command, but inspecting the output from the docker --version command is extra assurance that it is the docker executable that you expect instead of some other executable or script that shares the name. As for testing the exit code, I see that your edited question contains a solution. Feb 10, 2017 at 7:38
  • I think the docker --version would be better as you explained, but I don't know how to check for that because it is a string from which I want to get the version number (which should not be undefined). Feb 10, 2017 at 7:41
  • You would execute docker --version | grep "Docker version". If the exit code is 0, there is a successful match and the executable is likely the Docker executable. Feb 10, 2017 at 7:47
  • Oh, ok. Understand that. Is it possible to combine both into one? I have updated my post. As you can see my attempt looks very bad... :-( Feb 10, 2017 at 7:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .