9

I have some makefiles where most of the stuff should run without configuration. These makefiles have used docker-machine in the past.

Is there a way to detect in Bash if the user is using Docker Desktop for Mac instead of docker-machine?

nikolay
  • 2,565
  • 1
  • 21
  • 13
JE42
  • 4,881
  • 6
  • 41
  • 51
  • As noted in the official [getting started instructions](https://docs.docker.com/docker-for-mac/) it's no longer recommended to use `docker-machine` as it requires VirtualBox, as opposed to _Docker Desktop_ which uses the much faster **xhyve** and does not require VirtualBox to function. Consider refactoring your makefiles to reflect this. – vhs Oct 18 '19 at 09:43

5 Answers5

5

The cleanest way I found so far is:

[[ $(docker version --format "{{.Server.KernelVersion}}") == *-moby ]]
nikolay
  • 2,565
  • 1
  • 21
  • 13
  • 1
    Slight update: on 18.03, `KernelVersion` reports `4.9.87-linuxkit-aufs`, so one can check for `*-linuxkit-*` I guess. – Ciro Costa Apr 02 '18 at 12:14
2

The best way is to check for the existence of the DOCKER environment variables:

  • DOCKER_HOST
  • DOCKER_MACHINE_NAME
  • DOCKER_TLS_VERIFY
  • DOCKER_CERT_PATH

All four of these are set when eval $(docker-machine env) is run and are required for use with docker-machine.

The beta does not require any of them being set and in fact requires you to unset them in order to function properly.


You can also do a check in the docker info command looking for "moby" (the name of the docker for mac VM):

docker info | grep -q moby && echo "Docker for mac beta" || echo "Not docker for mac beta"

This is going to be dependent on consistency in the docker info results, however.

enderland
  • 13,825
  • 17
  • 98
  • 152
  • Yeah, i was thinking about this. However, i would prefer a positive condition checking for the existing of "Docker for mac" – JE42 Jul 06 '16 at 13:40
  • 1
    @JE42 I added another check for that, too, which is more of a positive. – enderland Jul 06 '16 at 13:56
2

You can open the terminal and just type docker info and it will give you the details about the docker if it is installed on your mac.

If it says command not found : docker then it means you don't have docker installed on your mac.

Nikunj Kakadiya
  • 2,689
  • 2
  • 20
  • 35
1

These makefiles have used docker-machine in the past.

If possible stop using Machine. Recent Docker for Mac installation instructions note Docker Toolbox but suggest it's legacy now. Consider refactoring your scripts to check for Docker Desktop instead:

if [[ -n "$(docker info --format '{{.OperatingSystem}}' | grep 'Docker Desktop')" ]]; then
    echo "Docker Desktop found. Skipping installation ..."
else
    echo "WARNING! Docker Desktop not installed:"
    echo "  * Install docker desktop from <https://docs.docker.com/docker-for-mac/install/>"
fi

Explaination

  • --format '{{.OperatingSystem}}' is a Go template which queries from docker info.
  • grep 'Docker Desktop' is what you should be looking for if you're running Docker Desktop.
  • -z returns true if the length of the string is 0 (from man bash).
  • $(...) is command substitution which executes the grep without outputting to stdout.

Hat tip to Joe for his answer which cleanly filters the output from docker info. And if you refactor to stop using Machine you can consider removing VirtualBox from your environment setup and leverage the performance gains of xhyve – included with Desktop.

vhs
  • 9,316
  • 3
  • 66
  • 70
0

You can do the following:

docker info --format "{{.OperatingSystem}}" | grep -q "Docker for Mac"

Check the exit code via:

if [[ $? -eq 0 ]]; then
    echo "Docker for Mac!"
else
    echo "Something else"
fi
vhs
  • 9,316
  • 3
  • 66
  • 70
Joe Alamo
  • 133
  • 1
  • 7
  • Use of `$?` [is an anti-pattern](https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts#comment92545215_27501200) and the `-q` is _clever_ which is adds complexity to debugging. – vhs Oct 18 '19 at 10:27