How can a script distinguish Docker Toolbox and Docker for Windows?

不想你离开。 提交于 2019-12-23 17:29:05

问题


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?


回答1:


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.




回答2:


#!/usr/bin/env bash

dockerIsToolBox() {
  if [ "${DOCKER_TOOLBOX_INSTALL_PATH}" ];then
    echo true
  else
    echo false
  fi
}


来源:https://stackoverflow.com/questions/43242218/how-can-a-script-distinguish-docker-toolbox-and-docker-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!