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
?
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
?
The best way is to check for the existence of the DOCKER environment variables:
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.
The cleanest way I found so far is:
[[ $(docker version --format "{{.Server.KernelVersion}}") == *-moby ]]
KernelVersion
reports 4.9.87-linuxkit-aufs
, so one can check for *-linuxkit-*
I guess. –
Outgoings The best way is to check for the existence of the DOCKER environment variables:
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.
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.
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.
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
$?
is an anti-pattern and the -q
is clever which is adds complexity to debugging. –
Tova 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
© 2022 - 2024 — McMap. All rights reserved.
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