How to check if Docker is installed in a Unix shell script?

Viewed 24967

I need to check in a shell script if Docker is installed (Ubuntu server).

I came up with this, but the syntax is not correct.

if [[ which docker && docker --version ]]; then
  echo "Update docker"
  # command
else
  echo "Install docker"
  # command
fi

I also tried if [ which docker ] && [ docker --version ]; then

2 Answers

Using suggestions from the answer in rickdenhaan's comment:

if [ -x "$(command -v docker)" ]; then
    echo "Update docker"
    # command
else
    echo "Install docker"
    # command
fi

This one is working for me:

if [[ $(which docker) && $(docker --version) ]]; then
    echo "Update docker"
    # command
  else
    echo "Install docker"
    # command
fi
Related