因国外对国内镜像站进行卡脖子操作,导致大部分的国内镜像站都无法进行访问,在拉取镜像时,都链接失败。所以今天搞个半永久的自己搭建镜像加速服务。
1.前置准备
1.1.域名
此方案需要准备一个域名才行,后续需要绑定到Cloudflare
中,域名购买的话,可以从腾讯云、百度云、阿里云、火山引擎(字节)里面选择域名注册即可。
找个最便宜的注册即可
2. cloudflare白嫖注册
首先进入官网地址dash.cloudflare.com/
接下来正常填写邮箱进行账号注册即可
3. 创建workers
在主页面中,选择左侧的workers and pages,然后新建即可,这里面没有什么过多的配置,默认走就可以了
点击部署即可
4. 网页代码编写
点击编写代码
4.1. 新建index.html
直接复制以下代码即可:
注意,docker的hub仓库地址要修改成自己的
<!DOCTYPE html>
<html lang="en">
<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": ["https://docker.xxoo.team"]
}
EOF
</pre>
</div>
<p>如果执行了上述命令,配置了镜像加速服务,可以直接 pull 镜像:</p>
<div class="code-block">
<pre>
docker pull halohub/halo:latest # 拉取 halo 镜像
</pre>
</div>
<p>因为Workers用量有限,在使用加速镜像服务时,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库:</p>
<div class="code-block">
<pre>
docker pull docker.xxoo.team/halohub/halo:latest # 拉取 halo 镜像
</pre>
</div>
</div>
</div>
<div class="footer">
<p>Powered by Cloudflare Workers</p>
<p><a href="https://www.xxoo.team">www.xxoo.team</a></p>
</div>
</body>
</html>
4.2. 修改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"
}
});
}
}
}
4.3. 点击部署即可
接下来等待即可
5. 域名绑定与dns解析
5.1. 创建网站
点击左侧目录中的网站,然后直接点击创建
5.2. 选择免费节点
5.3. 绑定dns
其中ipv4地址,随便填写
即可,3.3.3.3。然后进行保存就行。
点击继续即可
5.4. 名称服务器与购买域名地址进行绑定
复制框中的两个名称服务器地址,并且在域名商网站上进行绑定,以下介绍的是在火山引擎中如何绑定。
5.4.1. 复制名称服务器
5.4.2. 修改域名dns解析
在域名配置中,修改dns
服务器地址,为从5.4.1中复制的内容接口
比如:
dorthy.ns.cloudflare.com
rommy.ns.cloudflare.com
完成创建
之后点击完成创建即可
6. 绑定域名到workers中
因cloudflare默认提供的域名被墙,无法进行访问,所以才需要自己购买域名
把购买的域名,添加到设置-》触发器-》自定义中即可完成所有流程
浏览器输入自己的域名,出现如下界面代表转发成功。
在服务器中进行测试:
如图所示,已经拉取成功
7. 常见问题
7.1. 在workers中绑定域名时,出现目前 仅支持 cloudflare 上处于活动状态的域
该问题请检查步骤5中是否完成,因为cloudflare
绑定的域名,必须先在网站中绑定
7.2. docker1分钟内访问多次,在进行拉取时出现限制访问问题
这个目前没有解决办法,因为docker hub会限制陌生拉取次数。只能在等1分钟后进行拉取镜像
参考资料: