简介
鉴于当前的大部分 Docker 镜像加速库都纷纷遭到屏蔽或者被直接下架,致使在我们国内下载 Docker 镜像的困难程度正在日益增大。经过在网上进行资源的获取和查找,了解掌握到了能够通过 Cloud Flare 来免费搭建 Docker 镜像加速服务这样的途径,现在将其详细内容分享给大家如下。 不过需要注意每天每免费账号有次数限制,为10万次。每分钟为1000次
前期准备
- 一个Cloud Flare账号(注册很简单,网上教程多)
- 一个域名(随便一个云厂商的域名都可以)
托管域名
2、选择添加站点,因为我这里已经添加了一个所以会这样显示,如果没有站点的话下面会直接显示添加
3、然后在框内输入自己的域名,点击继续
4、选择free计划,点击继续
5、dns解析可以之后再填,这里直接点击继续
6、之后是把DNS服务器修改为cloud flare的服务器
7、这里以腾讯云为例,其他云厂商也差不多,可以在网上搜索到教程,进入控制台域名,域名信息页,点击修改DNS服务器
8、选择自定义DNS,输入上面的cloud flare的DNS服务器地址,点击提交即可
9、回到cloud flare页面,点击继续,会显示快速入门指南,这时候点以后完成就可以了
10、然后稍作等待,DNS服务器成功切换后,就会显示下面的界面,代表域名已经成功纳管了
新建Worker
1、进入Worker and Pages页面,点击创建
2、点击创建Worker
3、点击部署,等待部署完成
4、点击编辑代码
5、编辑Worker.js文件,将下面的代码全量替换上去
import HTML from './docker.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: "manual",
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"
}
});
}
}
}
6、新建docker.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);
}
.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>
</div>
<div class="container">
<div class="content">
<p>为了加速镜像拉取,你可以使用以下命令设置 registry mirror:</p>
<pre><code>sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://{{host}}"]
}
EOF</code></pre>
<p>为了避免 Worker 用量耗尽,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库:</p>
<pre><code>docker pull {{host}}/library/alpine:latest # 拉取 library 镜像
docker pull {{host}}/coredns/coredns:latest # 拉取 coredns 镜像</code></pre>
</div>
</div>
<div class="footer">
<p>Powered by Cloudflare Workers</p>
<p><a href="https://songxwn.com" target="_blank">访问博客 songxwn.com</a></p>
</div>
</body>
</html>
7、点击右上角的部署,保存并部署
8、进入Worker设置页,点击触发器,点击添加自定义域
9、输入自己的域名,可以输入一个二级域名,比如docker.yourdomain.com,然后点击添加自定义域
10、然后打开自己设置的域名,看到如下页面则说明成功,按照下面的方式替换镜像仓库即可