因为众所周知的原因,目前(2024-06-21)DockerHub在国内已经不可访问,国内大多镜像也无法使用。
本文将通过CloudFlare搭建一个自用的Docker Hub镜像。
注意:
需要独立的顶级域名,如果没有可以去阿里云、腾讯云等注册top域名,性价比高。
CloudFlare Worker 每天每免费账号有次数限制,为10万次。每分钟为1000次。对于个人用户来说也足够用了。
本文仅供学习技术交流,切勿用于非法用途!
在控制台->Workers和Pages > 创建 > 创建 Worker > 点击保存 >点击完成 > 编辑代码
给项目重新写个项目名,保存后点完成即可。
编辑代码的页面
新建docker.html文件
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>以下是两种使用示例:</p> <pre><code>docker pull {{host}}/library/alpine:latest # 拉取 library 镜像 docker pull nginx # 拉取 nginx 镜像</code></pre> </div> </div> <div class="footer"> <p>Powered by JiuCheng Studio</p> <p><a href="https://blog.minkse.cn" target="_blank">访问我的博客</a></p> </div> </body> </html>
在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" } }); } } }
点击部署后同时保存代码
默认情况下,在管理页面有一个默认的dev域名,但是国内基本上是访问不了。
这时候就需要添加自定义域名。
没有自定义域名则需要在网站里面去添加
一路点击继续或者下一步按钮
从网站->点击域名进入管理页面
找到DNS修改
我的域名是在阿里云注册的,在阿里云的控制面板里把DNS修改到cloudflare里
等待DNS生效(通常需要数十分钟)
然后在worker->设置->触发器->添加自定义域名
在浏览器里访问该域名即可
配置好docker的配置即可实现镜像效果。
配置完了之后需要重启docker服务
这样就可以愉快的下载docker镜像