国内无法访问下载Docker镜像的多种解决方案

您所在的位置:网站首页 tradingview国内用不了了吗 国内无法访问下载Docker镜像的多种解决方案

国内无法访问下载Docker镜像的多种解决方案

2024-07-17 09:12| 来源: 网络整理| 查看: 265

2023年5月中旬, hub.docker.com  "不知" 何种原因国内均无法正常访问了。当时只是官网不能访问,但是不影响pull镜像。

2024年6月,国内几家Docker Hub 镜像服务平台均被要求下架,停止服务。不知以后是否开放?或开启白名单模式?

这里不讨论其原因!这里分享几个方法,帮助童鞋如何正常的拉取Docker镜像!

如果童鞋你有更多方式,请留言分享给大家哈~

更多关于:Docker / Docker常用命令 / Docker Hub

 

零门槛境外镜像

优点:不需大量修改,只需几个命令

缺点:网络可能慢或者不稳定

有一个难兄难弟的国家:俄罗斯,被美国制裁无法顺利访问Docker Hub。这里可以借助俄罗斯国家的镜像!

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json   创建应用程序  -> 创建 Worker ->  点击保存 -> 点击完成 -> 编辑代码

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: requesthod,

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"

}

});

}

}

}

展开

 

新建文件 docker.html 粘贴下面内容

镜像使用说明

       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;

           }

       }

   

镜像使用说明

为了加速镜像拉取,你可以使用以下命令设置 registry mirror:

sudo tee /etc/docker/daemon.json ;;EOF

{

"registry-mirrors": ["https://{{host}}"]

}

EOF

为了避免 Worker 用量耗尽,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库:

docker pull {{host}}/library/alpine:latest # 拉取 library 镜像

docker pull {{host}}/coredns/coredns:latest # 拉取 coredns 镜像

Powered by Cloudflare Workers

展开

 

3,点击部署即可

4,绑定自定义域名

设置 -> 触发器 -> 自定义域 -> 点击【添加自定义域】

没有域名?访问博客历史文章:免费域名

演示地址:dockerhub.o0o.us.kg

 

Nginx反向代理

优点:需要有境外服务器

缺点:网络可能慢或者不稳定

这种方法需要自己有一台境外服务器,签发域名证书。按下面配置即可!

这里博主并未测试,下面内容仅供参考

server {

listen 443 ssl;

server_name 域名;

ssl_certificate 证书地址;

ssl_certificate_key 密钥地址;

proxy_ssl_server_name on; # 启用SNI

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;

error_page 429 = @handle_too_many_requests;

}

#处理重定向

location @handle_redirect {

resolver 1.1.1.1;

set $saved_redirect_location '$upstream_http_location';

proxy_pass $saved_redirect_location;

}

# 处理429错误

location @handle_too_many_requests {

proxy_set_header Host 替换为在CloudFlare Worker设置的域名;  # 替换为另一个服务器的地址

proxy_pass http://替换为在CloudFlare Worker设置的域名;

proxy_set_header Host $http_host;

}

}

展开

 

其他开源项目

https://github.com/NoCLin/LightMirrors

https://github.com/bboysoulcn/registry-mirror

 

最后总结

1,如临时使用,建议参与零门槛几个方案,方便快捷

2,据说后面pip源可能也会受到影响,可用采用HTTP代理方式

3,国内阿里云,腾讯云均提供私有镜像服务,大家可用尝试看看

版权声明:作者:我是小马甲~链接:https://51.ruyo.net/18687.html来源:如有乐享文章版权归作者所有,未经允许请勿转载。



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3