How can a script distinguish Docker Toolbox and Do

2019-07-14 18:40发布

On my current team, we're still transitioning from Docker Toolbox to Docker Desktop for Windows. A lot of our scripts still assume that you're running Docker Toolbox on VirtualBox (like how to mount drives, how slashes or drive names work for those mounts).

Is there a reliable way to tell, from inside a script, whether docker is coming from Docker Toolbox or Docker Desktop for Windows?

2条回答
放荡不羁爱自由
2楼-- · 2019-07-14 19:04
#!/usr/bin/env bash

dockerIsToolBox() {
  if [ "${DOCKER_TOOLBOX_INSTALL_PATH}" ];then
    echo true
  else
    echo false
  fi
}
查看更多
Emotional °昔
3楼-- · 2019-07-14 19:25

Toolbox works via docker-machine. The way the docker client is directed to the virtual machine is via a number of environment variables which you can see by running docker-machine env default

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=/user/.docker/machine/machines/default
SET DOCKER_MACHINE_NAME=default
REM Run this command to configure your shell: 
REM     @FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO @%i

Docker for Mac connects directly to the /var/run/docker.sock socket which is mapped into the Docker VM so this is easy to detect by the lack of environment variables.

I believe Docker for Windows uses a named pipe in the same way (//./pipe/docker_engine) so you should also be able to tell by the lack of DOCKER_HOST in the environment.

If Docker for Windows does still use the environment, there will be differences between the Toolbox and Docker for Windows variables. DOCKER_HOST would be on a different range. DOCKER_CERT_PATH won't include machine etc.

查看更多
登录 后发表回答