Reputation: 5169
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
?
Upvotes: 10
Views: 33862
Reputation: 11
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
Upvotes: 1
Reputation: 3008
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.
Upvotes: 3
Reputation: 10089
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.
Upvotes: 1
Reputation: 133
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
Upvotes: 0
Reputation: 2693
The cleanest way I found so far is:
[[ $(docker version --format "{{.Server.KernelVersion}}") == *-moby ]]
Upvotes: 5
Reputation: 14185
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.
Upvotes: 2