使用Docker toolbox时候如何使用国内registry mirror加速

最近在使用window上的docker toolbox

这个docker是基于virtual box虚拟机的+power shell架设的,自带图形管理Kitematic (Alpha)

但是众所周知,docker的官方HUB在国外,慢得1逼,国内的建议使用DAO Cloud的加速服务器对映像服务进行加速,但是因为多了一层VM,使操作docker的时候显得格外繁琐。现分享一点过程与思路:



虚拟机入口:

实际上执行的命令:


使用Docker toolbox时候如何使用国内registry mirror加速_第1张图片
"C:\Program Files\Git\bin\bash.exe" --login -i "C:\Program Files\Docker Toolbox\start.sh"

实际上运行的命令是:

"C:\Program Files\Git\bin\bash.exe" --login -i "C:\Program Files\Docker Toolbox\start.sh"

看上去像是用了power shell 执行Docker Toolbox的脚本了,那么前者没有价值,现在需要看看start.sh的内容:

#!/bin/bash

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

# TODO: I'm sure this is not very robust.  But, it is needed for now to ensure

# that binaries provided by Docker Toolbox over-ride binaries provided by

# Docker for Windows when launching using the Quickstart.

export PATH="/c/Program Files/Docker Toolbox:$PATH"

VM=${DOCKER_MACHINE_NAME-default}

DOCKER_MACHINE=./docker-machine.exe

STEP="Looking for vboxmanage.exe"

if [ ! -z "$VBOX_MSI_INSTALL_PATH" ]; then

VBOXMANAGE="${VBOX_MSI_INSTALL_PATH}VBoxManage.exe"

else

VBOXMANAGE="${VBOX_INSTALL_PATH}VBoxManage.exe"

fi

BLUE='\033[1;34m'

GREEN='\033[0;32m'

NC='\033[0m'

#clear all_proxy if not socks address

if  [[ $ALL_PROXY != socks* ]]; then

unset ALL_PROXY

fi

if  [[ $all_proxy != socks* ]]; then

unset all_proxy

fi

if [ ! -f "${DOCKER_MACHINE}" ]; then

echo "Docker Machine is not installed. Please re-run the Toolbox Installer and try again."

exit 1

fi

if [ ! -f "${VBOXMANAGE}" ]; then

echo "VirtualBox is not installed. Please re-run the Toolbox Installer and try again."

exit 1

fi

"${VBOXMANAGE}" list vms | grep \""${VM}"\" &> /dev/null

VM_EXISTS_CODE=$?

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 [ "${HTTP_PROXY}" ]; then

PROXY_ENV="$PROXY_ENV --engine-env HTTP_PROXY=$HTTP_PROXY"

fi

if [ "${HTTPS_PROXY}" ]; then

PROXY_ENV="$PROXY_ENV --engine-env HTTPS_PROXY=$HTTPS_PROXY"

fi

if [ "${NO_PROXY}" ]; then

PROXY_ENV="$PROXY_ENV --engine-env NO_PROXY=$NO_PROXY"

fi

"${DOCKER_MACHINE}" create -d virtualbox $PROXY_ENV --engine-registry-mirror http://*******.m.daocloud.io "${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})"

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

如上所述,这个脚本的功能就是:

先检查一下同名的叫做default的虚拟机是否已经在virtualbox里面存在

如果不存在就调用virtualbox的create api 新建一个,同时带上docker运行时候的参数,包含我们要设置的registry mirror

关于这个国内加速的registry mirror URL可以在https://www.daocloud.io/申请,详情不在叙述,再次感谢daocloud为国内docker社区推广和贡献

如上,把你的URL加到上面的create 语句中,docker设施就能生效

值得注意的是,因为这个只在create过程中设置,所以如果同名虚拟机已经存在,就不会再create,新的升值也不会生效。

你可能感兴趣的:(使用Docker toolbox时候如何使用国内registry mirror加速)