Docker Toolbox 搭配 VM 虚拟机

/ Developer / 没有评论 / 446浏览

安装脚本(Install.bat)

@echo off
color 0a

DockerToolbox-19.03.1.exe /TASKS="modifypath,upgradevm,desktopicon" /TYPE="custom" /COMPONENTS="Docker,DockerMachine,DockerCompose,!VirtualBox,!Kitematic,!Git"

echo f | xcopy start.sh "%DOCKER_TOOLBOX_INSTALL_PATH%\start.sh" /y>nul
echo f | xcopy docker-machine-driver-vmwareworkstation.exe "%DOCKER_TOOLBOX_INSTALL_PATH%\docker-machine-driver-vm.exe" /y>nul
echo f | xcopy docker-machine-driver-vmwareworkstation.exe "%DOCKER_TOOLBOX_INSTALL_PATH%\docker-machine-driver-vmwareworkstation.exe" /y>nul

mkdir "%USERPROFILE%\.docker\machine\cache" >nul
echo f | xcopy boot2docker_*.iso "%USERPROFILE%\.docker\machine\cache\boot2docker.iso" /y>nul

pause

启动脚本(start.sh)

#!/bin/bash

export PATH="$PATH:/d/Program Files (x86)/VMware/VMware Workstation"

trap '[ "$?" -eq 0 ] || read -p "Looks like something went wrong in step ´$STEP´... Press any key to continue..."' EXIT

VM=${DOCKER_MACHINE_NAME-default}
DOCKER_MACHINE=./docker-machine.exe

BLUE='\033[1;34m'
GREEN='\033[0;32m'
NC='\033[0m'


if [ ! -f "${DOCKER_MACHINE}" ]; then
  echo "Docker Machine is not installed. Please re-run the Toolbox Installer and try again."
  exit 1
fi


vmrun.exe list | grep ${VM} &> /dev/null
VM_EXISTS_CODE=$?
if [ $VM_EXISTS_CODE -eq 1 ]; then
  if [ -f ~/.docker/machine/machines/${VM}/${VM}.vmx ]; then
    vmrun.exe -T ws start ~/.docker/machine/machines/${VM}/${VM}.vmx nogui
    vmrun.exe list | grep ${VM} &> /dev/null
    VM_EXISTS_CODE=$?
  fi
fi

set -e

STEP="Checking if machine $VM exists"
if [ $VM_EXISTS_CODE -eq 1 ]; then
  "${DOCKER_MACHINE}" rm -f "${VM}" &> /dev/null || :
  rm -rf ~/.docker/machine/machines/"${VM}"
  #set proxy variables if they exists
  if [ -n ${HTTP_PROXY+x} ]; then
    PROXY_ENV="$PROXY_ENV --engine-env HTTP_PROXY=$HTTP_PROXY"
  fi
  if [ -n ${HTTPS_PROXY+x} ]; then
    PROXY_ENV="$PROXY_ENV --engine-env HTTPS_PROXY=$HTTPS_PROXY"
  fi
  if [ -n ${NO_PROXY+x} ]; then
    PROXY_ENV="$PROXY_ENV --engine-env NO_PROXY=$NO_PROXY"
  fi  
  "${DOCKER_MACHINE}" create -d vmwareworkstation $PROXY_ENV "${VM}"
fi

STEP="Checking status on $VM"
VM_STATUS="$(${DOCKER_MACHINE} status ${VM} 2>&1)"
if [ "${VM_STATUS}" != "Running" ]; then
  "${DOCKER_MACHINE}" start "${VM}"
  yes | "${DOCKER_MACHINE}" regenerate-certs "${VM}"
fi

STEP="Setting env"
eval "$("${DOCKER_MACHINE}" env --shell=bash --no-proxy "${VM}" | sed -e "s/export/SETX/g" | sed -e "s/=/ /g")" &> /dev/null #for persistent Environment Variables, available in next sessions
eval "$("${DOCKER_MACHINE}" env --shell=bash --no-proxy "${VM}")" #for transient Environment Variables, available in current session

STEP="Finalize"
clear
cat << EOF


                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/

EOF
echo -e "${BLUE}docker${NC} is configured to use the ${GREEN}${VM}${NC} machine with IP ${GREEN}$(${DOCKER_MACHINE} ip ${VM})${NC}"
echo "For help getting started, check out the docs at https://docs.docker.com"
echo
cd

docker () {
  MSYS_NO_PATHCONV=1 docker.exe "$@"
}
export -f docker

if [ $# -eq 0 ]; then
  echo "Start interactive shell"
  exec "$BASH" --login -i
else
  echo "Start shell with command"
  exec "$BASH" -c "$*"
fi