1、概述
Docker下载镜像,如果不指定地址的话,默认是从Docker官方提供的网站去下载镜像。官方提供的镜像服务器地址都是在国外,所以镜像下载速度会比较慢。
针对这种情况,国内有很多厂商提供镜像加速器的服务。
我们这里以阿里云镜像加速器为例来进行讲解。

在阿里云网站注册一个账号,会分配一个随机的阿里云镜像加速器的网址,然后把这个网址填入到/etc/docker/daemon.json,重启服务即可。
其实不用注册账号也行,上网搜索一个别人的镜像加速器的网址来用也可以。

下面通过“官方提供的”和“阿里云加速器”两个不同的方式来下载nginx镜像对比一下。

2、使用官方默认下载nginx镜像
在执行docker命令前加上time来进行计时,然后在另一个窗口使用tcpdump监听443端口,来查看访问的IP地址,可以用来判断后面加速器是否生效。

# time docker pull nginx

Using default tag: latest
latest: Pulling from library/nginx
b8f262c62ec6: Pull complete 
e9218e8f93b1: Pull complete 
7acba7289aa3: Pull complete 
Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

real	0m18.669s
user	0m0.097s
sys	0m0.082s

在另一个窗口执行tcpdump命令进行监听
# tcpdump -i ens32 tcp port 443

16:05:28.182770 IP ec2-34-228-211-243.compute-1.amazonaws.com.https > logserver.60568: Flags [.], ack 771, win 114, options [nop,nop,TS val 4241075396 ecr 3452509646], length 0
16:05:28.215575 IP ec2-52-55-198-220.compute-1.amazonaws.com.https > logserver.38941: Flags [S.], seq 29651911, ack 368058462, win 26847, options [mss 1460,sackOK,TS val 394692464 ecr 3452509656,nop,wscale 8], length 0
16:05:28.215727 IP logserver.38941 > ec2-52-55-198-220.compute-1.amazonaws.com.https: Flags [.], ack 1, win 229, options [nop,nop,TS val 3452509902 ecr 394692464], length 0
16:05:28.216519 IP logserver.38941 > ec2-52-55-198-220.compute-1.amazonaws.com.https: Flags [P.], seq 1:201, ack 1, win 229, options [nop,nop,TS val 3452509903 ecr 394692464], length 200
16:05:28.458713 IP ec2-52-55-198-220.compute-1.amazonaws.com.https > logserver.38941: Flags [.], ack 201, win 110, options [nop,nop,TS val 394692526 ecr 3452509903], length 0
16:05:28.458787 IP ec2-52-55-198-220.compute-1.amazonaws.com.https > logserver.38941: Flags [P.], seq 1:4097, ack 201, win 110, options [nop,nop,TS val 394692526 ecr 3452509903], length 4096
16:05:28.458920 IP logserver.38941 > ec2-52-55-198-220.compute-1.amazonaws.com.https: Flags [.], ack 4097, win 293, options [nop,nop,TS val 3452510145 ecr 394692526], length 0
16:05:28.460098 IP ec2-52-55-198-220.compute-1.amazonaws.com.https > logserver.38941: Flags [P.], seq 4097:5277, ack 201, win 110, options [nop,nop,TS val 394692526 ecr 3452509903], length 1180
16:07:07.711323 IP logserver.58767 > 104.18.123.25.https: Flags [.], ack 15917082, win 24530, options [nop,nop,sack 4 {16409102:16438302}{16400342:16406182}{16394502:16397422}{16366762:16393042}], length 0
16:07:07.713798 IP 104.18.123.25.https > logserver.58767: Flags [.], seq 16438302:16451442, ack 894, win 31, length 13140
16:07:07.713829 IP logserver.58767 > 104.18.123.25.https: Flags [.], ack 15917082, win 24530, options [nop,nop,sack 4 {16409102:16451442}{16400342:16406182}{16394502:16397422}{16366762:16393042}], length 0
16:07:07.716606 IP 104.18.123.25.https > logserver.58767: Flags [.], seq 16452902:16464582, ack 894, win 31, length 11680
16:07:07.716640 IP logserver.58767 > 104.18.123.25.https: Flags [.], ack 15917082, win 24530, options [nop,nop,sack 4 {16452902:16464582}{16409102:16451442}{16400342:16406182}{16394502:16397422}], length 0
16:07:07.719634 IP 104.18.123.25.https > logserver.58767: Flags [.], seq 16464582:16482102, ack 894, win 31, length 17520
16:07:07.719671 IP logserver.58767 > 104.18.123.25.https: Flags [.], ack 15917082, win 24530, options [nop,nop,sack 4 {16452902:16482102}{16409102:16451442}{16400342:16406182}{16394502:16397422}], length 0
16:07:07.722807 IP 104.18.123.25.https > logserver.58767: Flags [.], seq 16482102:16498162, ack 894, win 31, length 16060

可以看到,在下载镜像的过程有,有访问两个地址,一个是 ec2-52-55-198-220.compute-1.amazonaws.com,一个是104.18.123.25,两个IP都是美国的IP。

查看一下镜像的大小:
# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f949e7d76d63        9 hours ago         126MB

nginx镜像有126MB,而整个下载过程用时大概18秒,大概每秒7MB的速度。(这个速度大小也受本地主机的网速限制)

下面我们换成阿里云的加速器地址再测一下。

3、使用阿里云加速器下载nginx镜像

先注册一个阿里云账号https://www.aliyun.com/。
注册后,登录阿里云,在搜索框中搜索“容器镜像服务”,找到后,点击“立即开通”。

进入到容器镜像服务页面后,点击最下方的“镜像加速器”,里面就有加速器地址和配置说明。

根据它的说明配置Docker。

1. 安装/升级Docker客户端
推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce

2. 配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://自己的阿里云地址.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

重启服务后,我们先删除前面下载的nginx镜像。
# docker rmi nginx

然后再带上time执行下载镜像的命令,在另一个窗口中使用tcpdump监听443端口,验证加速器是否生效了。
# time docker pull nginx

Using default tag: latest
latest: Pulling from library/nginx
b8f262c62ec6: Pull complete 
e9218e8f93b1: Pull complete 
7acba7289aa3: Pull complete 
Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

real	0m15.807s
user	0m0.095s
sys	0m0.072s

# tcpdump -i ens32 tcp port 443

15:50:55.078530 IP logserver.49759 > 183.131.227.222.https: Flags [.], ack 13938359, win 22467, options [nop,nop,TS val 3451636765 ecr 1190876378], length 0
15:50:55.078706 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13938359:13939807, ack 1004, win 62, options [nop,nop,TS val 1190876379 ecr 3451636712], length 1448
15:50:55.079004 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13939807:13941255, ack 1004, win 62, options [nop,nop,TS val 1190876379 ecr 3451636713], length 1448
15:50:55.079044 IP logserver.49759 > 183.131.227.222.https: Flags [.], ack 13941255, win 22588, options [nop,nop,TS val 3451636766 ecr 1190876379], length 0
15:50:55.079587 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13941255:13942703, ack 1004, win 62, options [nop,nop,TS val 1190876380 ecr 3451636713], length 1448
15:50:55.079793 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13942703:13944151, ack 1004, win 62, options [nop,nop,TS val 1190876380 ecr 3451636713], length 1448
15:50:55.079904 IP logserver.49759 > 183.131.227.222.https: Flags [.], ack 13944151, win 22709, options [nop,nop,TS val 3451636767 ecr 1190876380], length 0
15:50:55.079953 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13944151:13945599, ack 1004, win 62, options [nop,nop,TS val 1190876381 ecr 3451636714], length 1448
15:50:55.080085 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13945599:13947047, ack 1004, win 62, options [nop,nop,TS val 1190876381 ecr 3451636714], length 1448
15:50:55.080192 IP logserver.49759 > 183.131.227.222.https: Flags [.], ack 13947047, win 22693, options [nop,nop,TS val 3451636767 ecr 1190876381], length 0
15:50:55.080274 IP 183.131.227.222.https > logserver.49759: Flags [.], seq 13947047:13948495, ack 1004, win 62, options [nop,nop,TS val 1190876381 ecr 3451636714], length 1448

可以看到,换成阿里云的地址后,连接的IP地址变成了183.131.227.222,查询这个IP地址的所属地,是“浙江省绍兴市 电信”。

下载镜像用时15秒,比使用默认的18秒少3秒,虽然效果不是非常好,但还是能说明阿里云镜像加速器生效了。