How can I detect if Docker for macOS is installed?
Asked Answered
A

6

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?

Adult answered 6/7, 2016 at 12:26 Comment(1)
As noted in the official getting started instructions 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.Tova
G
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.

Garlandgarlanda answered 6/7, 2016 at 13:20 Comment(2)
Yeah, i was thinking about this. However, i would prefer a positive condition checking for the existing of "Docker for mac"Adult
@Adult I added another check for that, too, which is more of a positive.Garlandgarlanda
M
5

The cleanest way I found so far is:

[[ $(docker version --format "{{.Server.KernelVersion}}") == *-moby ]]
Mabe answered 24/10, 2016 at 22:42 Comment(1)
Slight update: on 18.03, KernelVersion reports 4.9.87-linuxkit-aufs, so one can check for *-linuxkit-* I guess.Outgoings
G
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.

Garlandgarlanda answered 6/7, 2016 at 13:20 Comment(2)
Yeah, i was thinking about this. However, i would prefer a positive condition checking for the existing of "Docker for mac"Adult
@Adult I added another check for that, too, which is more of a positive.Garlandgarlanda
H
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.

Harwood answered 26/10, 2021 at 10:7 Comment(0)
T
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.

Tova answered 18/10, 2019 at 10:37 Comment(0)
B
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
Berners answered 13/3, 2018 at 13:40 Comment(1)
Use of $? is an anti-pattern and the -q is clever which is adds complexity to debugging.Tova
T
0

Check the version if it is installed or not. By that you will get whether it is installed or not on your machine. Type this in your terminal.

docker --version

Turkoman answered 14/10, 2023 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.