Cloudflare 搭建 Docker 镜像加速

新建Cloudflare Worker

1. worker.js

worker.js里填入以下内容:

import HTML from "./index.html";
export default {
    async fetch(request) {
        const url = new URL(request.url);
        const path = url.pathname;
        const originalHost = request.headers.get("host");
        const registryHost = "registry-1.docker.io";
        if (path.startsWith("/v2/")) {
        const headers = new Headers(request.headers);
        headers.set("host", registryHost);
        const registryUrl = `https://${registryHost}${path}`;
        const registryRequest = new Request(registryUrl, {
            method: request.method,
            headers: headers,
            body: request.body,
            redirect: "follow",
        });
        const registryResponse = await fetch(registryRequest);
        console.log(registryResponse.status);
        const responseHeaders = new Headers(registryResponse.headers);
        responseHeaders.set("access-control-allow-origin", originalHost);
        responseHeaders.set("access-control-allow-headers", "Authorization");
        return new Response(registryResponse.body, {
            status: registryResponse.status,
            statusText: registryResponse.statusText,
            headers: responseHeaders,
        });
        } else {
        return new Response(HTML.replace(/{{host}}/g, originalHost), {
            status: 200,
            headers: {
            "content-type": "text/html"
            }
        });
        }
    }
}

2. index.html

注:需要将 your.domain.com 替换为你自己真实可用的域名
新建index.html,并填入以下内容:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>镜像使用说明</title>
  <style>
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
      margin: 0;
      padding: 0;
      background-color: #f0f2f5;
      display: flex;
      flex-direction: column;
      min-height: 100vh;
    }
    .header {
      background: linear-gradient(90deg, #4e54c8 0%, #8f94fb 100%);
      color: white;
      text-align: center;
      padding: 20px 0;
    }
    .container {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 20px;
    }
    .content {
      background: white;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      padding: 20px;
      max-width: 800px; /* 调整后的宽度 */
      width: 100%;
      font-size: 16px; /* 放大字体 */
    }
    .code-block {
      background: #2d2d2d;
      color: #f8f8f2;
      padding: 10px;
      border-radius: 8px;
      margin: 10px 0;
      overflow-x: auto;
      font-family: "Courier New", Courier, monospace; /* 保持代码块的字体 */
    }
    .footer {
      background: #444;
      color: white;
      text-align: center;
      padding: 5px 0; /* 调低高度 */
    }
    .footer a {
      color: #4caf50;
      text-decoration: none;
    }
    @media (max-width: 600px) {
      .content {
        padding: 10px;
        font-size: 14px; /* 在小屏幕上稍微减小字体 */
      }
    }
  </style>
</head>
<body>
  <div class="header">
    <h1>镜像使用说明</h1>
  </div>
  <div class="container">
    <div class="content">
      <p>要设置加速镜像服务,你可以执行下面命令:</p>
      <div class="code-block">
        <pre>sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["your.domain.com"]
}
EOF</pre>
      </div>
      <p>如果执行了上述命令,配置了镜像加速服务,可以直接 pull 镜像:</p>
      <div class="code-block">
        <pre>docker pull ubuntu:latest # 拉取 ubuntu 镜像</pre>
      </div>
      <p>因为Workers用量有限,在使用加速镜像服务时,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库:</p>
      <div class="code-block">
        <pre>docker pull ubuntu:latest # 拉取 ubuntu 镜像</pre>
      </div>
    </div>
  </div>
  <div class="footer">
    <p>Powered by Cloudflare Workers</p>
    <p><a href="https://your.domain.com">your.domain.com</a></p>
  </div>
</body>
</html>

3. 部署

保存、部署该Worker,并添加your.domain.com自定义域指向该Worker

4. 配置

访问:https://your.domain.com即可访问向导页,修改目标PC上docker的配置文件daemon.json中的registry-mirrors["your.domain.com"],重启docker即可完成镜像加速配置

5. 测试

运行:docker pull ubuntu:latest即可测试是否实现镜像加速