docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

一、前言

先回顾下上一节创建docker-machine的过程,默认情况下docker toolbox中的docker-machine使用virtual box创建虚拟机,KI首次启动时创建虚拟机的过程,大致相当于下面这条命令:

docker-machine create --driver virtualbox default

输出如下:

Running pre-create checks...
Creating machine...
(default) Copying /Users/yjmyzz/.docker/machine/cache/boot2docker.iso to /Users/yjmyzz/.docker/machine/machines/default2/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect Docker to this machine, run: docker-machine env default

其大致过程为从网上下载boot2docker.iso这个文件,然后利用它创建VirtualBox VM,然后生成SSH key(方便免密码登录到虚拟机)、分配IP之类,创建完以后,可以用:

docker-machine env default

查看虚拟机default的状态,如果要删除该虚拟机,直接

rm -rf ~/.docker/machine/machines/default

现在才开始本文的正题:

 

二、使用vmware做为docker machine

对于已经在mac上安装了vmwarefusion的同学来讲,为了使用docker强制安装一个virtualbox有点多余,其实docker支持vmwarefusion,只要把driver改一下就行了,命令如下:

docker-machine create --driver vmwarefusion default

创建完成后,用docker-machine ls列出所有虚拟机

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

可以看到类型确实为vmwarefusion,然后可以

docker-machine ssh default

连接到虚拟机的终端下,顺便ping下外网地址,检查下虚拟机里是否能上网(这个很重要,因为后面下载镜像需要联网)

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速  

如果有条件翻*^墙的同学,可以运行

docker pull kitematic/hello-world-nginx 

直接从docker hub拉一个只有几M的镜像文件试试

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

 

三、设置国内镜像加速pull

docker hub官网太慢了,还好国内有一家公司daocloud提供了加速服务,设置步骤:

3.1 先到daocloud.io网站注册一个账号

过程略,注册成功后,进入控制台

3.2 点击控制台上的加速器

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

拉到中间部分,有一个『主机监控程序』的文字链接,见下图:

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

然后选择主机类型,我用的是mac,所以选择mac主机:

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

如果已经安装好了docker toolbox最新版,直接点击【安装好了】

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

然后会出现二条命令,复制粘贴执行即可:

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

注:如果按它的提示,最后出现latest: Pulling from daocloud/daomonit not found之类的错误,可以尝试

docker logout daocloud.io

然后重试,安装成功后,执行

dao pull ubuntu

就可以感受下速度了

docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速  

如果好奇dao这个命令是什么鬼?可以

which dao

查找下位置,正常情况下应该在/usr/local/bin/dao下,可以cat看下该文件的内容:

#!/bin/sh

# DaoTools made by DaoCloud

# Update
command_exists() {
	command -v "$@" > /dev/null 2>&1
}
lsb_dist=''
if command_exists lsb_release; then
	lsb_dist="$(lsb_release -si)"
fi
if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then
	lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
lsb_dist="$(echo $lsb_dist | cut -d " " -f1)"
lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"

sh_c='sh -c'

if [ "$user" != 'root' ]; then
	if command_exists sudo; then
		sh_c='sudo -E sh -c'
	elif command_exists su; then
		sh_c='su -c'
	else
		echo >&2 'Error: dao needs the ability to run commands as root.'
		echo >&2 'We are unable to find either "sudo" or "su" available to make this happen.'
	fi
fi


update_daotools() {
	## Update Bash from https://get.daocloud.io/daotools

	## Update Docker Image
	$sh_c "docker inspect daocloud.io/daocloud/daocloud-toolset" > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	  $sh_c "docker pull daocloud.io/daocloud/daocloud-toolset"
	fi

	$sh_c "curl --retry 20 --retry-delay 5 -L -o /tmp/daotools https://get.daocloud.io/daotools"
	if grep -q "DaoCloud" /tmp/daotools
	then
		$sh_c "chmod +x /tmp/daotools"
		if [  "$lsb_dist" = "coreos"  ]
		then
			$sh_c "mv -f /tmp/daotools /opt/bin/dao"
		else
			$sh_c "mv -f /tmp/daotools /usr/local/bin/dao"
		fi
	fi

#	if [ "$(uname)" == "Darwin" ]
#	then
#		chmod 777 /usr/local/bin/dao
#	fi




}



# RUN

$sh_c "docker inspect daocloud.io/daocloud/daocloud-toolset" > /dev/null 2>&1
if [ $? -eq 1 ]; then
	echo "Dao from DaoCloud"
	echo "Initializing, Please wait a minute"
	$sh_c "docker pull daocloud.io/daocloud/daocloud-toolset"
	if [ $? -eq 0 ]; then
		echo "Inital Success"
		echo
	fi
fi

# UPDATE

update_daotools > /dev/null 2>&1  &


# check if this is a tty mode
tty > /dev/null 2>&1

if [ $? -eq 0 ]
then
        istty="-it"
else
        istty=""
fi

if [ "$(uname)" = "Darwin" ]
then
	$sh_c "docker run --rm -v /var/run/docker.sock:/var/run/docker.sock  -v $(which docker):/usr/bin/docker:ro \
	  -v /mnt/sda1/daocloud:/etc/daocloud -v /tmp/daocloud:/tmp/daocloud $istty daocloud.io/daocloud/daocloud-toolset $*"
elif [ "$lsb_dist" = "boot2docker" ]
then
	$sh_c "docker run --rm -v /var/run/docker.sock:/var/run/docker.sock  -v $(which docker):/usr/bin/docker:ro \
	  -v /mnt/sda1/daocloud:/etc/daocloud -v /tmp/daocloud:/tmp/daocloud $istty daocloud.io/daocloud/daocloud-toolset $*"
else
	$sh_c "docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker:ro \
	  -v /etc/daocloud:/etc/daocloud -v /tmp/daocloud:/tmp/daocloud --privileged $istty daocloud.io/daocloud/daocloud-toolset $*"

有兴趣的可以仔细研究研究

 

参考文章:

http://elasticskies.com/docker-machine-on-osx-with-vmware-fusion/

上一篇:深入 CSocket 编程之阻塞和非阻塞模式


下一篇:maven学习九之pom.xml或settings.xml对nexus的配置