3

在我目前的团队中,我们仍在从Docker Toolbox过渡到Docker Desktop for Windows。我们的许多脚本仍然假设您在 VirtualBox 上运行 Docker Toolbox(例如如何挂载驱动器,斜杠或驱动器名称如何为这些挂载工作)。

有没有一种可靠的方法可以从脚本内部判断docker是来自 Docker Toolbox 还是 Docker Desktop for Windows?

4

2 回答 2

4
#!/usr/bin/env bash

dockerIsToolBox() {
  if [ "${DOCKER_TOOLBOX_INSTALL_PATH}" ];then
    echo true
  else
    echo false
  fi
}
于 2018-05-23T15:08:57.810 回答
3

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.

于 2017-04-06T02:59:48.683 回答