基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务
本文主要介绍了如何基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建 dockerhub、gcr、quay 等镜像加速服务。
最近,受限于各种情况,部分主流镜像站都关了,为了能够正常使用,建议自己搭建一个加速器。
写文之前,也已经部署好了一个,可以直接使用,具体使用方法跳转 https://docker.lixd.xyz 或者 使用说明 查看。
准备工作
- 1)首先要注册一个 Cloudflare 账号
- 2)Cloudflare 账号下需要添加一个域名,推荐到万网注册一个域名,再转移到 Cloudflare。
- 推荐 top、xyz 后缀应该都是首年 7 块
部署流程
修改配置
首先 fork https://github.com/ciiiii/cloudflare-docker-proxy 仓库到自己账号下。
然后修改 src/index.js,修改内容如下:
const routes = {
"docker.mydomain.com": "https://registry-1.docker.io",
"quay.mydomain.com": "https://quay.io",
"gcr.mydomain.com": "https://gcr.io",
"k8s-gcr.mydomain.com": "https://k8s.gcr.io",
"k8s.mydomain.com": "https://registry.k8s.io",
"ghcr.mydomain.com": "https://ghcr.io",
"cloudsmith.mydomain.com": "https://docker.cloudsmith.io",
};
其中 mydomain.com 就是你的域名,比如我这里用的是 lixd.xyz
再修改 wrangler.toml,同样是替换域名
[env.production]
name = "cloudflare-docker-proxy"
routes = [
{ pattern = "docker.mydomain.com", custom_domain = true },
{ pattern = "quay.mydomain.com", custom_domain = true },
{ pattern = "gcr.mydomain.com", custom_domain = true },
{ pattern = "k8s-gcr.mydomain.com", custom_domain = true },
{ pattern = "k8s.mydomain.com", custom_domain = true },
{ pattern = "ghcr.mydomain.com", custom_domain = true },
{ pattern = "cloudsmith.mydomain.com", custom_domain = true },
]
[env.production.vars]
MODE = "production"
TARGET_UPSTREAM = ""
[env.staging]
name = "cloudflare-docker-proxy-staging"
route = { pattern = "docker-staging.mydomain.com", custom_domain = true }
同样的 mydomain.com 就是你的域名
最后修改 README.md 中的 图标对应的 Github 仓库地址为你 fork 后的仓库地址,比如 https://deploy.workers.cloudflare.com/?url=https://github.com/lixd/cloudflare-docker-proxy。
这三个修改都完成后,提交代码。
帮助文档
为了便于使用,可以在访问根目录时返回一个帮助页面。
help.html
新建一个help.html 文件,内容如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>镜像使用说明</title>
<style>
body {
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.header {
background: linear-gradient(135deg, #667eea, #764ba2);
color: #fff;
padding: 20px 0;
text-align: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
position: relative;
}
.github-link {
position: absolute;
top: 10px;
right: 20px;
color: #fff;
text-decoration: none;
}
.github-icon {
width: 24px;
height: 24px;
vertical-align: middle;
}
.container {
max-width: 800px;
margin: 40px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.content {
margin-bottom: 20px;
}
.footer {
text-align: center;
padding: 20px 0;
background-color: #333;
color: #fff;
}
pre {
background-color: #272822;
color: #f8f8f2;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
code {
font-family: 'Source Code Pro', monospace;
}
a {
color: #4CAF50;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
@media (max-width: 600px) {
.container {
margin: 20px;
padding: 15px;
}
.header {
padding: 15px 0;
}
}
</style>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Source+Code+Pro:wght@400;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="header">
<h1>镜像使用说明</h1>
<a href="https://github.com/lixd/cloudflare-docker-proxy" target="_blank" class="github-link">
<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" alt="GitHub" class="github-icon">
</a>
</div>
<div class="container">
<div class="content">
<p>为了加速 Docker 镜像拉取,你可以使用以下命令设置 registry mirror:</p>
<pre><code id="registry-config">sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://{{host}}"]
}
EOF
# 配置完后需要重启 Docker 服务
sudo systemctl restart docker
</code></pre>
<p>使用该代理从不同的镜像仓库拉取镜像,请参考以下命令:</p>
<pre><code id="commands">
# docker pull nginx:latest
docker pull docker.{{host}}/library/nginx:latest # 拉取 Docker 官方镜像
# docker pull quay.io/coreos/etcd:latest
docker pull quay.{{host}}/coreos/etcd:latest # 拉取 Quay 镜像
# docker pull gcr.io/google-containers/busybox:latest
docker pull gcr.{{host}}/google-containers/busybox:latest # 拉取 GCR 镜像
# docker pull k8s.gcr.io/pause:latest
docker pull k8s-gcr.{{host}}/pause:latest # 拉取 k8s.gcr.io 镜像
# docker pull registry.k8s.io/pause:latest
docker pull k8s.{{host}}/pause:latest # 拉取 registry.k8s.io 镜像
# docker pull ghcr.io/github/super-linter:latest
docker pull ghcr.{{host}}/github/super-linter:latest # 拉取 GitHub 容器镜像
# docker pull docker.cloudsmith.io/public/repo/image:latest
docker pull cloudsmith.{{host}}/public/repo/image:latest # 拉取 Cloudsmith 镜像
</code></pre>
<p>为了避免 Worker 用量耗尽,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库。</p>
</div>
</div>
<div class="footer">
<p>Powered by Cloudflare Workers</p>
<p><a href="https://lixueduan.com" target="_blank">访问博客 探索云原生</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const host = window.location.hostname;
const mainDomain = host.split('.').slice(-2).join('.');
const registryConfigElement = document.getElementById('registry-config');
const commandsElement = document.getElementById('commands');
registryConfigElement.innerHTML = registryConfigElement.innerHTML.replace(/{{host}}/g, host);
commandsElement.innerHTML = commandsElement.innerHTML.replace(/{{host}}/g, mainDomain);
});
</script>
</body>
</html>
增加路由
scr/index.js 中增加一条路由,访问根目录时就返回这个帮助页面
import DOCS from './help.html'
// return docs
if (url.pathname === "/") {
return new Response(DOCS, {
status: 200,
headers: {
"content-type": "text/html"
}
});
}
完整内容见:https://github.com/lixd/cloudflare-docker-proxy
点击部署
然后在 Github 界面点击这个 图标进行部署,会自动跳转到 cloudflare,按步骤操作即可,最终会在 Github 仓库中创建一个 Github Action 来将该仓库部署到 Cloudflare Workers。
就像这样:

等待部署完成
可以到 Github 查看 Action 执行进度

执行完成后,切换到 Cloudflare Dashboard ,不出意外的话就可以看到刚创建的 Worker 了。

切换到 Setting,等待 SSL 证书签发完成即可

使用说明
部署完成后,访问 https://docker.mydomain.com 就可以看到使用说明了。
为了加速 Docker 镜像拉取,你可以使用以下命令设置 registry mirror:
sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://docker.lixd.xyz"]
}
EOF
# 配置完后需要重启 Docker 服务
sudo systemctl restart docker
使用该代理从不同的镜像仓库拉取镜像,请参考以下命令:
# docker pull nginx:latest
docker pull docker.lixd.xyz/library/nginx:latest # 拉取 Docker 官方镜像
# docker pull quay.io/coreos/etcd:latest
docker pull quay.lixd.xyz/coreos/etcd:latest # 拉取 Quay 镜像
# docker pull gcr.io/google-containers/busybox:latest
docker pull gcr.lixd.xyz/google-containers/busybox:latest # 拉取 GCR 镜像
# docker pull k8s.gcr.io/pause:latest
docker pull k8s-gcr.lixd.xyz/pause:latest # 拉取 k8s.gcr.io 镜像
# docker pull registry.k8s.io/pause:latest
docker pull k8s.lixd.xyz/pause:latest # 拉取 registry.k8s.io 镜像
# docker pull ghcr.io/github/super-linter:latest
docker pull ghcr.lixd.xyz/github/super-linter:latest # 拉取 GitHub 容器镜像
# docker pull docker.cloudsmith.io/public/repo/image:latest
docker pull cloudsmith.lixd.xyz/public/repo/image:latest # 拉取 Cloudsmith 镜像
为了避免 Worker 用量耗尽,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库。
FAQ
Build & Deploy The process '/usr/local/bin/yarn' failed with exit code 1 +1
✘ [ERROR] A request to the Cloudflare API (/accounts/***/workers/scripts/cloudflare-docker-proxy/domains/records) failed.
workers.api.error.origin_conflict_existing_dns_record [code: 100117]
如果提前添加了 DNS 解析则出现这个错误,部署之后会自动添加解析记录,因此部署前不要手动添加记录。
如果出现该问题,可以把 DNS 解析记录删除后再试。
如果你对云原生技术充满好奇,想要深入了解更多相关的文章和资讯,欢迎关注微信公众号。
搜索公众号【探索云原生】即可订阅

参考
白嫖Cloudflare Workers 搭建 Docker Hub镜像加速服务|
又发掘一个CF的新用法,利用Worker构建Docker Registry Mirror
基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务的更多相关文章
- Linux 使用 docker 下搭建xunsearch 搜索引擎服务
Linux 使用 docker 下搭建 xunsearch 搜索引擎服务 安装 docker 环境(菜鸟教程有说明) 安装docker说明 下载并运行 xunsearch 的服务端:docker安装x ...
- Docker 中国官方镜像加速
参考:https://www.docker-cn.com/registry-mirror 通过 Docker 官方镜像加速,中国区用户能够快速访问最流行的 Docker 镜像.该镜像托管于中国大陆,本 ...
- Docker拉取镜像加速
关于Docker拉取镜像加速 打开桌面 docker 小图标 选中框框 根据下图 添加国内的加速源即可 Docker加速源 #网易 http://hub-mirror.c.163.com #Docke ...
- Docker+Gogs搭建个人Git服务
欢迎 经常使用Github的我,Github它功能强大,操作简单,不用FQ,所以大家会使用Github进行代码托管,但是,Github的私仓收费的,而且对于普通个人用户来说,价格也不便宜.很多人搭建自 ...
- docker 基础之镜像加速
国内访问 Docker Hub 有时会遇到困难,此时可以配置镜像加速器 对于使用 systemd 的系统,用 systemctl enable docker 启用服务后,编辑 /etc/systemd ...
- ubuntu 安装 docker 并配置镜像加速(使用 apt-get 进行安装)
ubuntu 安装docker CentOS docker安装 https://blog.csdn.net/weixin_44953227/article/details/108597310 你需要这 ...
- CentOS Docker安装、镜像加速
CentOS Docker安装 方法一:使用官方安装脚本自动安装 # 安装命令: curl -fsSL https://get.docker.com | bash -s docker --mirror ...
- docker(五) 使用Docker Registry搭建镜像私服
1.创建私服 docker run -d --name registry -v /opt/data/registry:/var/lib/registry -p 5000:5000 registry - ...
- Kubernetes搭建过程中使用k8s.gcr.io、quay.io、docker.io的镜像加速
前言 因为众所周知的原因,在使用Kubernetes和docker的时候会出现一些镜像无法拉取或者速度较慢的情况,错误信息类似以下: [ERROR ImagePull]: failed to pull ...
- docker使用自定义镜像zabbix服务
一.关闭firewall,永久关闭,使用iptables防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firew ...
随机推荐
- [python] 基于PyWaffle库绘制华夫饼图
华夫饼图Waffle chart是一种独特而直观的图表,用于表示分类数据.它采用网格状排列的等大小方格或矩形,每个方格或矩形分配不同的颜色或阴影来表示不同的类别.这种可视化方法有效地传达了每个类别在整 ...
- pikachu靶机练习平台-xss
第一题:反射性xss(get) 输出的字符出现在url中 第二题:反射性xss(post) 登录后输入<script>alert(1)</script> 第三题:存储型xss ...
- python 操作xls
目录 写入文件 demo01 demo02 写入文件 demo01 # 读取:xlrd # 写入:xlwt # 修改(追加写入):xlutils import xlrd import xlwt fro ...
- Docker服务搭建个人音乐播放器Koel(及马里奥游戏)
Koel简介 Koel是一种简单的基于Web的个人音频流服务,用客户端的Vue和服务器端的Laravel编写.针对Web开发人员,Koel采用了一些更现代的Web技术来完成其工作 搭建步骤 docke ...
- ContextCapture-硬件配置推荐
ContextCapture倾斜摄影的空三计算.三维建模应用.非常耗费硬件资源,适当调整硬件配置,可以显著提高模型处理时间. 硬件常见问题 随着倾斜摄影建模算法成熟,应用越来越广泛,数据量越来越大,需 ...
- 同时添加多个的远程桌面工具,Windows远程桌面设置多用户同时登录
Windows Server 版本上的 Windows 远程桌面服务 (RDS) 允许多个用户同时登录. 但是,在标准的Windows桌面版本(例如Windows 10)上,默认情况下,远程桌面是为单 ...
- 前端 PM 分享:PM 需要做的事情
个人经验分享 PM PM( Project Manager ) PM( Product Manager ) 一.什么情况下需要前端担任 PM? 在我之前遇到的项目中,大多数项目的 PM 是由后端/产品 ...
- Springboot+Mybatis+Clickhouse+jsp 搭建单体应用项目(二)(添加日志打印和源码地址)
一.添加yaml设置 1 logging: 2 level: 3 com.mrliu.undertow.mapper : debug 二.添加pom的Hutool工具,完善日志打印处理 1 <d ...
- 线程中使用for循环的add或remove方法的两种方案
简介 (Introduction): 背景 在使用线程中添加list的元素时,使用add或remove就会产生异常. 分析 该list每当删除/添加一个元素时,集合的size方法的值都会减小1,这将直 ...
- CentOS7 升级 curl 到 HTTP2
目录 文章目录 目录 编译安装 YUM 升级 编译安装 安装编译环境: yum -y groupinstall "Development Tools" yum -y install ...