如何使用
修改 Docker 配置文件
直接修改 Docker 的配置文件,以便永久使用该镜像源。
- 打开 Docker 配置文件,路径通常为
/etc/docker/daemon.json
,没有就新建一个。 - 添加或修改以下内容:
{
"registry-mirrors": [
"https://dc.j8.work",
"https://docker.registry.cyou",
"https://docker.hlyun.org",
"https://docker.chenby.cn",
"https://docker.jsdelivr.fyi",
"https://docker-mirrors.mjjman.com",
"https://docker.kubesre.xyz",
"https://huecker.io",
"https://dockerhub.timeweb.cloud",
"https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://dockercf.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi"
]
}
- 保存文件并重启 Docker 服务:
sudo systemctl restart docker
现有加速网址
https://docker.1t.al
https://dc.j8.work
https://docker.registry.cyou
https://docker.hlyun.org
https://docker.chenby.cn
https://docker.jsdelivr.fyi
https://docker-mirrors.mjjman.com
https://docker.kubesre.xyz
https://huecker.io
https://dockerhub.timeweb.cloud
https://docker.registry.cyou
https://docker-cf.registry.cyou
https://dockercf.jsdelivr.fyi
https://dockertest.jsdelivr.fyi
自建加速
使用 CloudFlare Worker 搭建
将 worker.js 的内容替换为下面内容
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"
}
});
}
}
}
使用 Nginx 搭建
#反代docker hub镜像源
server {
listen 443 ssl;
server_name 域名;
ssl_certificate 证书地址;
ssl_certificate_key 密钥地址;
ssl_session_timeout 24h;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
location / {
proxy_pass https://registry-1.docker.io; # Docker Hub 的官方镜像仓库
proxy_set_header Host registry-1.docker.io;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 关闭缓存
proxy_buffering off;
# 转发认证相关的头部
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
# 对 upstream 状态码检查,实现 error_page 错误重定向
proxy_intercept_errors on;
# error_page 指令默认只检查了第一次后端返回的状态码,开启后可以跟随多次重定向。
recursive_error_pages on;
# 根据状态码执行对应操作,以下为301、302、307状态码都会触发
error_page 301 302 307 = @handle_redirect;
}
location @handle_redirect {
resolver 1.1.1.1;
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。