cloudflare-docker-proxy

cloudflare-docker-proxy

使用 Cloudflare Workers 搭建 Docker 代理

简介

ℹ️
2024年端午节6月6号后,因为神秘原因,很多镜像源纷纷下架失效。

本文记录如何使用 Cloudflare Workers 自行搭建一个 docker 镜像源

步骤

fork repo

原仓库:https://github.com/ciiiii/cloudflare-docker-proxy

https://github.com/viacooky/cloudflare-docker-proxy

修改 src/index.js

<your_sub_domain> 换成你的域名,如:docker.example.com gcr.example.com

src/index.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 这里修改需要代理的服务,我只代理了 dockerhub

const dockerHub = "https://registry-1.docker.io";

const routes = {
"<your_sub_domain>": dockerHub,
// "<your_sub_domain>": "https://quay.io",
// "<your_sub_domain>": "https://gcr.io",
// "<your_sub_domain>": "https://k8s.gcr.io",
// "<your_sub_domain>": "https://registry.k8s.io",
// "<your_sub_domain>": "https://ghcr.io",
// "<your_sub_domain>": "https://docker.cloudsmith.io",
// "<your_sub_domain>": "https://public.ecr.aws",
// // staging
// "<your_sub_domain>": dockerHub,
};

可参考:src/index.js

修改 wrangler.toml

<your_sub_domain> 换成你的域名,如:docker.example.com gcr.example.com

wrangler.toml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 对应 src/index.js 修改

[env.production]
name = "cloudflare-docker-proxy"
routes = [
  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
#  { pattern = "<your_sub_domain>", custom_domain = true },
]

添加关于页面(可选)

创建 about.html

若需要增加首页说明,可以新建 src/about.html ,并自行修改

src/about.html
src/about.html
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<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&amp;family=Source+Code+Pro:wght@400;700&amp;display=swap" rel="stylesheet">
</head>
<body>
<div class="header">
    <h1>欢迎使用本站加速 Docker 镜像拉取
</h1>
    <a href="https://github.com/viacooky/cloudflare-docker-proxy" target="_blank" class="github-link">
        
    </a>
</div>
<div class="container">
    <div class="content">
        

<center><h1>使用方法</h1></center>



<h2><p>1. 创建或修改 <code>/etc/docker/daemon.json</code> 文件,修改为:</p></h2>
        <pre><code id="registry-config">{
    "registry-mirrors": ["https://docker.0-0.im"]
}
</code></pre>
<h2><p>2. 重启docker</p></h2>
<pre><code id="registry-config">sudo systemctl restart docker.service</code></pre>
        
        
        <br>

<br>
<center><p>自建镜像,切勿滥用</p></center>
    </div>
</div>
<div class="footer">
    <p>Powered by Cloudflare Workers</p>
    <p><a href="https://0-0.im" target="_blank">0-0.im</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>

可参考:

添加路由

src/index.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 引入 about.html
import DOCS from './about.html'
 
// return docs

// 增加路由,Path为 / 的时候,返回 about.html
if (url.pathname === "/") {
  return new Response(DOCS, {
    status: 200,
    headers: {
      "content-type": "text/html"
    }
  });
}

可参考:src/index.js

部署

<your_repo_url> 改为你的仓库地址,并中浏览器中访问,一步步绑定即可。

https://deploy.workers.cloudflare.com/?url=<your_repo_url>

如:
https://deploy.workers.cloudflare.com/?url=https://github.com/viacooky/cloudflare-docker-proxy

或使用源仓库的部署方式也可以

cloudflare-docker-proxy

使用

修改 docker 源

创建或修改 /etc/docker/daemon.json 文件

<your_domain> 换成你的域名,如:docker.example.com gcr.example.com

/etc/docker/daemon.json
1
2
3
{
    "registry-mirrors": ["<your_domain>"]
}

也可以执行以下命令

<your_domain> 换成你的域名,如:docker.example.com gcr.example.com

shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 创建目录
sudo mkdir -p /etc/docker

# 创建并写入文件
sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["<your_domain>"]
}
EOF

# 重载daemon
sudo systemctl daemon-reload

# 重启docker
sudo systemctl restart docker.service

测速

可以拉取 node:latest 镜像测试速度

shell
1
docker pull node:latest

本地测试,速度大约 10MB/s

参考

https://www.lixueduan.com/posts/docker/12-docker-mirror/

https://github.com/lixd/cloudflare-docker-proxy