无法直连 DockerHub 下载镜像的代理方案整理

✨ 由于 Docker Hub 的镜像服务受到限制,用户可以通过配置 Docker 镜像仓库加速来解决下载问题。主要步骤包括修改/etc/docker/daemon.json中的registry-mirrors地址,使用提供的多个镜像加速地址,并提供了相关的命令示例。还提到使用 Cloudflare 进行自建镜像加速的注意事项,以及一些开源项目的链接供参考。此外,文中还介绍了 Docker 镜像的离线导出与导入方法。

近期 Docker Hub 的镜像服务平台均“被”关停了各自的服务,阿里云 / 华为云 等云厂商的私有镜像加速也不能被使用了。

如下为上海交大镜像站声明的存档: 

https://web.archive.org/web/20240606081039/https://sjtug.org/post/mirror-news/2024-06-06-takedown-dockerhub/

作为个人使用,我们可以直接全局代理或分流处理网络流量,从而拥有真-互联网的体验。但在某些场景下,我们还是不得不使用到一些其他的加速方案,如下对其进行一个简单的整理:

我们要配置 Docker 镜像仓库加速,核心就是修改 /etc/docker/daemon.json 中 registry-mirrors 的镜像加速地址。

镜像加速地址镜像说明
https://docker.registry.cyouhttps://t.me/bestcfipas/1900
https://docker-cf.registry.cyouhttps://t.me/bestcfipas/1900
https://docker.1panel.live1panel 面板镜像服务
https://hub.rat.dev耗子面板镜像服务
https://dockerhub.o0o.us.kghttps://dockerhub.o0o.us.kg
https://dockerhub.icuhttps://dockerhub.icu/

 Docker daemon 配置代理

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.registry.cyou",
        "https://docker-cf.registry.cyou"
    ]
}
EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

配置完成后使用如下:

# 拉取 library 镜像
docker pull docker.registry.cyou/library/nginx:alpine
# 拉取 coredns 镜像
docker pull docker.registry.cyou/coredns/coredns:latest

镜像拉取后,可重新 tag 的来修改:

# 重新 tag
docker tag docker.registry.cyou/coredns/coredns:latest coredns:latest
# 移除原 tag
docker rmi docker.registry.cyou/coredns/coredns:latest
上面拉取镜像时用到的library 是一个特殊的命名空间,它代表的是官方镜像。如果是某个用户的镜像就把library替换为镜像的用户名即可。

👇 如果自建镜像加速可查阅下方 v2ex 的帖子,其中 3 楼回复有针对 library 的处理,有需要可参考。

Cloudflare 自建加速镜像

http://v2ex.com/t/1007922
⚠️
用 Cloudflare 来代理镜像仓库的方案请酌情谨慎使用,据不可靠消息官方已经开始对此类大量滥用行为进行账号封禁处理。

自建镜像相关开源项目

https://github.com/dqzboy/Docker-Proxyhttps://github.com/ciiiii/cloudflare-docker-proxyhttps://github.com/cmliu/CF-Workers-docker.iohttps://github.com/ImSingee/hammalhttps://github.com/bboysoulcn/registry-mirrorhttps://github.com/wzshiming/crproxy/https://github.com/NoCLin/LightMirrors

附:Docker 镜像离线导出与导入

# 在可联网主机上拉取镜像
docker pull nginx
# 导出镜像为 tar 包
docker save -o nginx.tar nginx:alpine
# 将上面的 tar 包传输至另外的主机,再导入镜像
docker load -i ./nginx.tar