背景说明
使用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-master
Running 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.net
docker@deephash-master:~$ cd /etc/docker
docker@deephash-master:/etc/docker$ ls
key.json
docker@deephash-master:/etc/docker$ 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: deephash-master
ID: CTFY:5RUH:GVZC:ZKLP:XI2H:6MJJ:PFGG:V7MB:6LAX:7KUA:HXPA:YF4Q
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:
https://wf45d3ay.mirror.aliyuncs.com/
Live Restore Enabled: false
Product License: Community Engine
docker@deephash-master:/etc/docker$
docker@deephash-master:/etc/docker$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
docker@deephash-master:/etc/docker$ exit
logout
[root@vm1 ~]#
修改配置
进入docker machine并修改配置文件
[root@vm1 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
default - virtualbox Running tcp://192.168.99.103:2376 v19.03.12
hostvm - virtualbox Stopped Unknown
manage - virtualbox Stopped Unknown
[root@vm1 ~]# docker-machine ssh default
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@default:~$ vi /var/lib/boot2docker/profile
编辑内容
EXTRA_ARGS='
--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
为如下内容
EXTRA_ARGS='
--label provider=virtualbox
--registry-mirror=http://xiqingongzi.m.alauda.cn
'
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-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:~$