背景说明

使用Docker Machine时会存在两个和镜像加速的地方。

解决方案

book2docker

镜像缓存

docker运行 docker-machine create 命令第一次启动会去github.com下载一个最新的boot2docker.iso 镜像,国内下载会很慢,有时会卡死。
访问github:https://github.com/boot2docker/boot2docker/releases/
image.png
并将文件放置至目录~/.docker/machine/cache/

指定镜像

—virtualbox-boot2docker-url手动指定了boot2docker.iso位置。如果不指定该参数,则会从网络直接下载最新版本,非常缓慢

  1. docker-machine create --driver virtualbox --virtualbox-boot2docker-url ~/.docker/machine/cache/boot2docker.iso myvm1

image

指定参数

Docker进程在pull镜像时不指定镜像加速器也会特别的慢,这里指定阿里云

  1. [root@vm1 ~]# docker-machine create --engine-registry-mirror https://wf45d3ay.mirror.aliyuncs.com -d virtualbox deephash-master
  2. Running pre-create checks...
  3. Creating machine...
  4. (deephash-master) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/deephash-master/boot2docker.iso...
  5. (deephash-master) Creating VirtualBox VM...
  6. (deephash-master) Creating SSH key...
  7. (deephash-master) Starting the VM...
  8. (deephash-master) Check network to re-create if needed...
  9. (deephash-master) Waiting for an IP...
  10. Waiting for machine to be running, this may take a few minutes...
  11. Detecting operating system of created instance...
  12. Waiting for SSH to be available...
  13. Detecting the provisioner...
  14. Provisioning with boot2docker...
  15. Copying certs to the local machine directory...
  16. Copying certs to the remote machine...
  17. Setting Docker configuration on the remote daemon...
  18. Checking connection to Docker...
  19. Docker is up and running!
  20. To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env deephash-master
  21. [root@vm1 ~]# docker-machine ssh deephash-master
  22. ( '>')
  23. /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
  24. (/-_--_-\) www.tinycorelinux.net
  25. docker@deephash-master:~$ cd /etc/docker
  26. docker@deephash-master:/etc/docker$ ls
  27. key.json
  28. docker@deephash-master:/etc/docker$ docker info
  29. Client:
  30. Debug Mode: false
  31. Server:
  32. Containers: 0
  33. Running: 0
  34. Paused: 0
  35. Stopped: 0
  36. Images: 0
  37. Server Version: 19.03.12
  38. Storage Driver: overlay2
  39. Backing Filesystem: extfs
  40. Supports d_type: true
  41. Native Overlay Diff: true
  42. Logging Driver: json-file
  43. Cgroup Driver: cgroupfs
  44. Plugins:
  45. Volume: local
  46. Network: bridge host ipvlan macvlan null overlay
  47. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  48. Swarm: inactive
  49. Runtimes: runc
  50. Default Runtime: runc
  51. Init Binary: docker-init
  52. containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
  53. runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
  54. init version: fec3683
  55. Security Options:
  56. seccomp
  57. Profile: default
  58. Kernel Version: 4.19.130-boot2docker
  59. Operating System: Boot2Docker 19.03.12 (TCL 10.1)
  60. OSType: linux
  61. Architecture: x86_64
  62. CPUs: 1
  63. Total Memory: 985.4MiB
  64. Name: deephash-master
  65. ID: CTFY:5RUH:GVZC:ZKLP:XI2H:6MJJ:PFGG:V7MB:6LAX:7KUA:HXPA:YF4Q
  66. Docker Root Dir: /mnt/sda1/var/lib/docker
  67. Debug Mode: false
  68. Registry: https://index.docker.io/v1/
  69. Labels:
  70. provider=virtualbox
  71. Experimental: false
  72. Insecure Registries:
  73. 127.0.0.0/8
  74. Registry Mirrors:
  75. https://wf45d3ay.mirror.aliyuncs.com/
  76. Live Restore Enabled: false
  77. Product License: Community Engine
  78. docker@deephash-master:/etc/docker$
  79. docker@deephash-master:/etc/docker$ docker pull nginx
  80. Using default tag: latest
  81. latest: Pulling from library/nginx
  82. a2abf6c4d29d: Pull complete
  83. a9edb18cadd1: Pull complete
  84. 589b7251471a: Pull complete
  85. 186b1aaa4aa6: Pull complete
  86. b4df32aa5a72: Pull complete
  87. a0bcbecc962e: Pull complete
  88. Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
  89. Status: Downloaded newer image for nginx:latest
  90. docker.io/library/nginx:latest
  91. docker@deephash-master:/etc/docker$ exit
  92. logout
  93. [root@vm1 ~]#

修改配置

进入docker machine并修改配置文件

  1. [root@vm1 ~]# docker-machine ls
  2. NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
  3. deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
  4. default - virtualbox Running tcp://192.168.99.103:2376 v19.03.12
  5. hostvm - virtualbox Stopped Unknown
  6. manage - virtualbox Stopped Unknown
  7. [root@vm1 ~]# docker-machine ssh default
  8. ( '>')
  9. /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
  10. (/-_--_-\) www.tinycorelinux.net
  11. docker@default:~$ vi /var/lib/boot2docker/profile

编辑内容

  1. EXTRA_ARGS='
  2. --label provider=virtualbox
  3. '
  4. CACERT=/var/lib/boot2docker/ca.pem
  5. DOCKER_HOST='-H tcp://0.0.0.0:2376'
  6. DOCKER_STORAGE=overlay2
  7. DOCKER_TLS=auto
  8. SERVERKEY=/var/lib/boot2docker/server-key.pem
  9. SERVERCERT=/var/lib/boot2docker/server.pem

为如下内容

  1. EXTRA_ARGS='
  2. --label provider=virtualbox
  3. --registry-mirror=http://xiqingongzi.m.alauda.cn
  4. '
  5. CACERT=/var/lib/boot2docker/ca.pem
  6. DOCKER_HOST='-H tcp://0.0.0.0:2376'
  7. DOCKER_STORAGE=overlay2
  8. DOCKER_TLS=auto
  9. SERVERKEY=/var/lib/boot2docker/server-key.pem
  10. 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:~$