基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务

cloudflare,workers,docker,proxy · 浏览次数 : 0

小编点评

本文主要介绍了如何基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建 Docker Hub、GCR、Quay 等镜像加速服务。为了应对一些主流镜像站点关闭的情况,建议自己搭建一个加速器。文章详细阐述了准备工作、部署流程、修改配置、帮助文档等方面的内容,并提供了详细的操作步骤和常见问题解答。 1. **准备工作**:首先需要注册一个 Cloudflare 账号并添加一个域名,推荐使用 top、xyz 等后缀,以便在 Cloudflare 上部署 worker。 2. **部署流程**:fork 一个 Cloudflare-docker-proxy 仓库到自己的账号下,修改 src/index.js 和 wrangler.toml 文件,以及 README.md 文件中的 Github 仓库地址。 3. **修改配置**:在 src/index.js 中修改 routes 数组,添加你的域名及其对应的镜像仓库地址;修改 wrangler.toml 文件,设置 MODE、TARGET_UPSTREAM 等参数。 4. **帮助文档**:新建一个 help.html 文件,提供使用说明和常见问题解答。 5. **常见问题解答**:包括 DNS 解析记录的处理、Cloudflare API 错误等问题的解决方法。 通过以上步骤,可以成功搭建一个 Docker Hub、GCR、Quay 等镜像加速服务。文章还提供了一个简单的教程链接,供读者参考。

正文

本文主要介绍了如何基于 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 中的 Deploy to Cloudflare Workers 图标对应的 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 &lt;&lt;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 界面点击这个 Deploy to Cloudflare Workers 图标进行部署,会自动跳转到 cloudflare,按步骤操作即可,最终会在 Github 仓库中创建一个 Github Action 来将该仓库部署到 Cloudflare Workers。

就像这样:

deploy-to-cloudflare.png

等待部署完成

可以到 Github 查看 Action 执行进度

deploy-progress.png

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

cloudflare-workers.png

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

cloudflare-workers-setting.png

使用说明

部署完成后,访问 https://docker.mydomain.com 就可以看到使用说明了。

比如:https://docker.lixd.xyz

为了加速 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-docker-proxy

白嫖Cloudflare Workers 搭建 Docker Hub镜像加速服务|

又发掘一个CF的新用法,利用Worker构建Docker Registry Mirror

Docker 镜像库国内加速的几种方法

与基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务相似的内容:

基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务

本文主要介绍了如何基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建 dockerhub、gcr、quay 等镜像加速服务。 最近,受限于各种情况,部分主流镜像站都关了,为了能够正常使用,建议自己搭建一个加速器。 写文之前,也已经部署好了一个,可以直

[转帖]NGINX 局限太多,Cloudflare 最终放弃它并用 Rust 自研了全新替代品

https://www.infoq.cn/news/s2fa603MsEENsCmibTYI 长期以来,NGINX 可以说是网站安全和托管服务提供商 Cloudflare 的核心,是其所使用的基础软件的一部分。 “Cloudflare 将 NGINX 用于其提供的所有 Web 服务,并在世界各地的数

基于 .net core 8.0 的 swagger 文档优化分享-根据命名空间分组显示

之前也分享过 Swashbuckle.AspNetCore 的使用,不过版本比较老了,本次演示用的示例版本为 .net core 8.0,从安装使用开始,到根据命名空间分组显示,十分的有用

跟我一起学习和开发动态表单系统-前端用vue、elementui实现方法(3)

基于 Vue、Element UI 和 Spring Boot + MyBatis 的动态表单系统前端实现解析 在现代企业信息系统中,动态表单是一种非常常见的功能。它可以根据业务需求灵活地调整表单结构,以满足不同的数据收集和展示需求。在本文中,我们将探讨一种基于 Vue、Element UI 和 S

基于Bootstrap Blazor开源的.NET通用后台权限管理系统

前言 今天大姚给大家分享一个基于Bootstrap Blazor开源的.NET通用后台权限管理系统,后台管理页面兼容所有主流浏览器,完全响应式布局(支持电脑、平板、手机等所有主流设备),可切换至 Blazor 多 Tabs 模式,权限控制细化到网页内任意元素(按钮、表格、文本框等等):Bootstr

基于Chrome扩展的浏览器可信事件与网页离线PDF导出

基于Chrome扩展的浏览器可信事件与网页离线PDF导出 Chrome扩展是一种可以在浏览器中添加新功能和修改浏览器行为的软件程序,我们可以基于Manifest规范的API实现对于浏览器和Web页面在一定程度上的修改,例如广告拦截、代理控制等。Chrome DevTools Protocol则是Ch

基于cifar数据集合成含开集、闭集噪声的数据集

前言 噪声标签学习下的一个任务是:训练集上存在开集噪声和闭集噪声;然后在测试集上对闭集样本进行分类。 训练集中被加入的开集样本,会被均匀得打上闭集样本的标签充当开集噪声;而闭集噪声的设置与一般的噪声标签学习一致,分为对称噪声:随机将闭集样本的标签替换为其他类别;和非对称噪声:将闭集样本的标签替换为特

基于 JuiceFS 构建高校 AI 存储方案:高并发、系统稳定、运维简单

中山大学的 iSEE 实验室(Intelligence Science and System) Lab)在进行深度学习任务时,需要处理大量小文件读取。在高并发读写场景下,原先使用的 NFS 性能较低,常在高峰期导致数据节点卡死。此外,NFS 系统的单点故障问题也导致一旦数据节点宕机,该机器上的数据将

基于Python和TensorFlow实现BERT模型应用

本文分享自华为云社区《使用Python实现深度学习模型:BERT模型教程》,作者: Echo_Wish。 BERT(Bidirectional Encoder Representations from Transformers)是Google提出的一种用于自然语言处理(NLP)的预训练模型。BERT

Vite-Wechat网页聊天室|vite5.x+vue3+pinia+element-plus仿微信客户端

基于Vue3+Pinia+ElementPlus仿微信网页聊天模板Vite5-Vue3-Wechat。 vite-wechat使用最新前端技术vite5+vue3+vue-router@4+pinia+element-plus搭建网页端仿微信界面聊天系统。包含了聊天、通讯录、朋友圈、短视频、我的等功