Docker Hub镜像拉取限速

Docker Hub镜像拉取限速

上一篇Docker开始收费了么发布之后,有同学问了这样一个问题。“我不注册docker hub,不登陆,它怎么限制我拉取镜像的次数”,今天就快速回答一下这个问题

Docker Hub镜像拉取限速

一旦超过,镜像拉取就会失败,报错信息如下

ERROR: toomanyrequests: Too Many Requests.

或者

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

匿名用户拉取次数的限制

对于匿名用户,这个限制是通过IP地址进行的。

为了确保匿名用户,进行下面的实验之前,请先运行一下

docker logout

docker提供了一个API可以查看你最近6小时用了多少镜像拉取,还剩下多少次可以用,方法如下(我这里通过Linux)

TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token)
curl --head -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest

你会获取到这样的结果

HTTP/1.1 200 OK
content-length: 2782
content-type: application/vnd.docker.distribution.manifest.v1+prettyjws
docker-content-digest: sha256:767a3815c34823b355bed31760d5fa3daca0aec2ce15b217c9cd83229e0e2020
docker-distribution-api-version: registry/2.0
etag: "sha256:767a3815c34823b355bed31760d5fa3daca0aec2ce15b217c9cd83229e0e2020"
date: Sat, 04 Sep 2021 19:33:21 GMT
strict-transport-security: max-age=31536000
ratelimit-limit: 100;w=21600
ratelimit-remaining: 100;w=21600

ratelimit-limit: 100;w=21600 意思是每6小时(21600秒)你可以拉取镜像100次

ratelimit-remaining: 100;w=21600 意思是还剩下100次

拉几次镜像后,再查看,就会看待剩余次数的减少(比如下面98次)

ratelimit-limit: 100;w=21600
ratelimit-remaining: 98;w=21600

这个时候如果你使用家里的其它电脑匿名拉取一下镜像,你会看到剩余次数再次减少

ratelimit-limit: 100;w=21600
ratelimit-remaining: 97;w=21600

注意,这个API的token失效时间非常短,如果获取失败,请重新生成token

登录用户的拉取次数限制

通过 docker login 登录一下,然后再次查看次数

注意,重新生成token,加上你的docker hub用户名和密码

➜  ~ TOKEN=$(curl --user 'username:password' "https://auth.docker.io/token?service=registry.docker.io&scope=repositor
y:ratelimitpreview/test:pull" | jq -r .token)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4476    0  4476    0     0   7161      0 --:--:-- --:--:-- --:--:--  7150
➜  ~ curl --head -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest
HTTP/1.1 200 OK
content-length: 2782
content-type: application/vnd.docker.distribution.manifest.v1+prettyjws
docker-content-digest: sha256:767a3815c34823b355bed31760d5fa3daca0aec2ce15b217c9cd83229e0e2020
docker-distribution-api-version: registry/2.0
etag: "sha256:767a3815c34823b355bed31760d5fa3daca0aec2ce15b217c9cd83229e0e2020"
date: Sat, 04 Sep 2021 19:49:22 GMT
strict-transport-security: max-age=31536000
ratelimit-limit: 200;w=21600
ratelimit-remaining: 200;w=21600

➜  ~

限制次数就变成每6小时200次了。

参考资料

https://docs.docker.com/docker-hub/download-rate-limit/#how-do-i-authenticate-pull-requests
https://www.docker.com/increase-rate-limits
https://www.docker.com/blog/checking-your-current-docker-pull-rate-limits-and-status/

Discussion