0%

解决镜像加速失效问题

1. 基础资源准备

  • 1个 Cloudflare 账户
  • 1个域名(没有可以自己去网上免费申请或在 Cloudflare 上购买)

2. 创建应用程序

alt text

2.1 创建 Worker

  • 点击 Workers 和 Pages -> 创建应用程序 -> 创建 Worker -> 点击 保存 -> 点击 完成 -> 编辑代码
  • 创建2个文件,编写代码后保存 -> 选择 布署 -> 保存并布署
    alt text

2.2 创建 worker.js 文件

将以下代码复制到 worker.js 文件中,并保存:

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
128
129
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: "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.3 创建 docker.html 文件
将以下代码复制到 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://xxx.com"] #这里改成xxx.com 网页上就显示xxx.com
}
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>
</body>
</html>

3. 布署服务

布署完后,配置触发器并添加自定义域名。

3.1 国内域名处理(可选)

alt text
如果使用非cloudflare提供的域名,则需要进行激活,如国内则需要指定dns解析服务器,等节点刷新cloudflare根据cname的方式激活域名,最后则可以将work节点的服务以域名的形式映射访问。

4. 修改镜像服务器的配置文件

修改要拉取镜像的服务器 /etc/docker/daemon.json 文件:

json
复制代码
{
“registry-mirrors”: [“https://xxx.com"] # 自己的域名地址,浏览器可以直接打开该网址说明没问题
}

5. 重新加载配置并重启 Docker 服务

bash
复制代码
systemctl daemon-reload && systemctl restart docker

6. 拉取镜像验证

bash
复制代码
docker pull tomcat:8.0.52

Welcome to doublezOnline!

Hello, I am a backend developer. This blog is intended to share and record knowledge, and also to encourage myself to always keep learning and stay passionate. I remember a saying: The best time to plant a tree was ten years ago. The second best time is now..

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment