背景说明
使用Docker Machine时会存在两个和镜像加速的地方。
解决方案
book2docker
镜像缓存
docker运行 docker-machine create 命令第一次启动会去github.com下载一个最新的boot2docker.iso 镜像,国内下载会很慢,有时会卡死。
访问github:https://github.com/boot2docker/boot2docker/releases/
并将文件放置至目录~/.docker/machine/cache/
指定镜像
—virtualbox-boot2docker-url手动指定了boot2docker.iso位置。如果不指定该参数,则会从网络直接下载最新版本,非常缓慢
docker-machine create --driver virtualbox --virtualbox-boot2docker-url ~/.docker/machine/cache/boot2docker.iso myvm1
image
指定参数
Docker进程在pull镜像时不指定镜像加速器也会特别的慢,这里指定阿里云
[root@vm1 ~]# docker-machine create --engine-registry-mirror https://wf45d3ay.mirror.aliyuncs.com -d virtualbox deephash-masterRunning pre-create checks...Creating machine...(deephash-master) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/deephash-master/boot2docker.iso...(deephash-master) Creating VirtualBox VM...(deephash-master) Creating SSH key...(deephash-master) Starting the VM...(deephash-master) Check network to re-create if needed...(deephash-master) Waiting for an IP...Waiting for machine to be running, this may take a few minutes...Detecting operating system of created instance...Waiting for SSH to be available...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 your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env deephash-master[root@vm1 ~]# docker-machine ssh deephash-master( '>')/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.(/-_--_-\) www.tinycorelinux.netdocker@deephash-master:~$ cd /etc/dockerdocker@deephash-master:/etc/docker$ lskey.jsondocker@deephash-master:/etc/docker$ docker infoClient:Debug Mode: falseServer:Containers: 0Running: 0Paused: 0Stopped: 0Images: 0Server Version: 19.03.12Storage Driver: overlay2Backing Filesystem: extfsSupports d_type: trueNative Overlay Diff: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins:Volume: localNetwork: bridge host ipvlan macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary: docker-initcontainerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429runc version: dc9208a3303feef5b3839f4323d9beb36df0a9ddinit version: fec3683Security Options:seccompProfile: defaultKernel Version: 4.19.130-boot2dockerOperating System: Boot2Docker 19.03.12 (TCL 10.1)OSType: linuxArchitecture: x86_64CPUs: 1Total Memory: 985.4MiBName: deephash-masterID: CTFY:5RUH:GVZC:ZKLP:XI2H:6MJJ:PFGG:V7MB:6LAX:7KUA:HXPA:YF4QDocker Root Dir: /mnt/sda1/var/lib/dockerDebug Mode: falseRegistry: https://index.docker.io/v1/Labels:provider=virtualboxExperimental: falseInsecure Registries:127.0.0.0/8Registry Mirrors:https://wf45d3ay.mirror.aliyuncs.com/Live Restore Enabled: falseProduct License: Community Enginedocker@deephash-master:/etc/docker$docker@deephash-master:/etc/docker$ docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxa2abf6c4d29d: Pull completea9edb18cadd1: Pull complete589b7251471a: Pull complete186b1aaa4aa6: Pull completeb4df32aa5a72: Pull completea0bcbecc962e: Pull completeDigest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latestdocker@deephash-master:/etc/docker$ exitlogout[root@vm1 ~]#
修改配置
进入docker machine并修改配置文件
[root@vm1 ~]# docker-machine lsNAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORSdeephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12default - virtualbox Running tcp://192.168.99.103:2376 v19.03.12hostvm - virtualbox Stopped Unknownmanage - virtualbox Stopped Unknown[root@vm1 ~]# docker-machine ssh default( '>')/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.(/-_--_-\) www.tinycorelinux.netdocker@default:~$ vi /var/lib/boot2docker/profile
编辑内容
EXTRA_ARGS='--label provider=virtualbox'CACERT=/var/lib/boot2docker/ca.pemDOCKER_HOST='-H tcp://0.0.0.0:2376'DOCKER_STORAGE=overlay2DOCKER_TLS=autoSERVERKEY=/var/lib/boot2docker/server-key.pemSERVERCERT=/var/lib/boot2docker/server.pem
为如下内容
EXTRA_ARGS='--label provider=virtualbox--registry-mirror=http://xiqingongzi.m.alauda.cn'CACERT=/var/lib/boot2docker/ca.pemDOCKER_HOST='-H tcp://0.0.0.0:2376'DOCKER_STORAGE=overlay2DOCKER_TLS=autoSERVERKEY=/var/lib/boot2docker/server-key.pemSERVERCERT=/var/lib/boot2docker/server.pem
执行命令docker-machine restart default等待重启完成后,就可以使用docker pull命令来下载镜像。
以上操作也可以直接通过命令替换
docker@default:~$ sed -i "s|EXTRA_ARGS='|EXTRA_ARGS='--registry-mirror=http://xiqingongzi.m.alauda.cn |g" /var/lib/boot2docker/profile
docker@default:~$ cat /var/lib/boot2docker/profile
EXTRA_ARGS='--registry-mirror=http://xiqingongzi.m.alauda.cn
--label provider=virtualbox
'
CACERT=/var/lib/boot2docker/ca.pem
DOCKER_HOST='-H tcp://0.0.0.0:2376'
DOCKER_STORAGE=overlay2
DOCKER_TLS=auto
SERVERKEY=/var/lib/boot2docker/server-key.pem
SERVERCERT=/var/lib/boot2docker/server.pem
docker@default:~$ exit
[root@vm1 ~]# docker-machine restart default
[root@vm1 ~]# docker-machine ssh default
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@default:~$ docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.130-boot2docker
Operating System: Boot2Docker 19.03.12 (TCL 10.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 985.4MiB
Name: default
ID: GJLG:ERFX:C2XW:WZMT:RGKV:XMAC:YWVY:OX4N:QBGN:TKUZ:VN4F:KMX3
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
http://xiqingongzi.m.alauda.cn/
Live Restore Enabled: false
Product License: Community Engine
docker@default:~$
