背景

自从Docker从11月份开始对dockerhub限速之后,频繁碰到限速的反馈。限速信息如下:

You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limits

免费帐号会限制 200 pulls/6小时,匿名帐号则限制 100 pulls/6小时。

下文中提供一种搭建私有镜像registry方式来彻底解决这个烦恼。

方法

由于国内网络环境问题,我们经常使用镜像加速器,虽然可以提升镜像下载速度,但是每次都会从外网拉镜像。速度相对仍然较慢。

在这里我们配置内网的镜像仓库,第一次从外网拉取镜像后,以后每次从本地启动,下载速度可以突破10M。

首先我们新建一个VPC,给VPC配置EIP,让其从外网拉取镜像。在其中一台主机上部署私有仓库,其余节点配置从该仓库拉取。流量走内部私有网络,除了第一次有点慢,之后每次速度都超快,而且告别了Docker镜像拉取限制。

部署私有仓库

选定私有仓库部署主机,如:该主机IP 192.168.0.10

  1. 首先创建个本地volume,持久化镜像文件
docker volume create docker-repo
  1. 使用容器启动服务
docker run --name=cache -d --restart=always docker-repo:/var/lib/registry -p 5000:5000 registry:2

现在已经启动了一个本地registry,监听端口为5000。此时的Registry地址为:192.168.0.10:5000

使用方法

在其余主机上配置如下:

# cat /etc/docker/daemon.json
{
        "registry-mirrors": [
                "http://192.168.0.10:5000"
        ]
}

reload docker

systemctl reload docker

这样每次会从该registry拉镜像

可以查看registry的镜像容量大小:

# docker volume inspect docker-repo
[
    {
        "CreatedAt": "2020-11-25T22:58:18+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/docker-repo/_data",
        "Name": "docker-repo",
        "Options": {},
        "Scope": "local"
    }
]
# ls /var/lib/docker/volumes/docker-repo/_data
docker  scheduler-state.json

# ls /var/lib/docker/volumes/hub-cache/_data/docker/ | du -sh
2.1G	.

只有当该registry没有该镜像的时候会从外网拉取一次,以后全部走缓存,极大的减少了向dockerhub的拉取次数,从而顺利解决dockerhub限速限次数问题。

8 个月 后
8 天 后

海燕呐,你可用点心吧,少了个-v,还少了个变量:

docker run -d --name registry-cache \
  --restart=always \
  -p 5000:5000 \
  -e REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io \
  -v docker-repo:/var/lib/registry \
  registry:2

配置registry-mirrors

# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["http://192.168.93.9:5000"]
}

systemctl daemon-reload
systemctl restart docker

[root@k8s-node3 ~]# docker info --format '{{ .RegistryConfig.Mirrors }}' 
[http://192.168.93.9:5000/]

第一次拉取测试36s

[root@k8s-node3 ~]# docker rmi centos

[root@k8s-node3 ~]# time docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

real    0m36.138s
user    0m0.105s
sys     0m0.098s

此时已经自动缓存了

[root@localhost ~]# ls /var/lib/docker/volumes/docker-repo/_data/docker/registry/v2/repositories/library/
centos  nginx

第二次拉取测试9s,快乐4倍

[root@k8s-node3 ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859

[root@k8s-node3 ~]# time docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

real    0m9.018s
user    0m0.074s
sys     0m0.074s
    7 天 后