各种版本的linux换源、docker换源

参考国内镜像状态 2024年7月2日:

镜像加速器镜像加速器地址专属加速器其它加速
Docker 中国官方镜像https://registry.docker-cn.comDocker Hub已关闭
DaoCloud 镜像站https://docker.m.daocloud.io白名单模式Docker Hub、GCR、K8S、GHCR、Quay、NVCR 等
Azure 中国镜像https://dockerhub.azk8s.cn仅供内部访问Docker Hub、GCR、Quay
科大镜像站https://docker.mirrors.ustc.edu.cn仅供内部访问Docker HubGCRQuay
阿里云https://<your_code>.mirror.aliyuncs.com需登录,系统分配Docker Hub
七牛云https://reg-mirror.qiniu.comDocker Hub、GCR、Quay
网易云https://hub-mirror.c.163.comDocker Hub
腾讯云https://mirror.ccs.tencentyun.com仅供内部访问Docker Hub
Docker 镜像代理https://dockerproxy.comDocker Hub、GCR、K8S、GHCR
百度云https://mirror.baidubce.comDocker Hub
南京大学镜像站https://docker.nju.edu.cnDocker Hub、GCR、GHCR、Quay、NVCR 等
上海交大镜像站https://docker.mirrors.sjtug.sjtu.edu.cnDocker Hub、GCR 等已关闭
中科院软件所镜像站https://mirror.iscas.ac.cnDocker Hub

1. 南大是个好选择

南大除了docker hub 下不来,其他的仓库如github的仓库gcr可以下,参考1,官网

2. 自建镜像

参考 https://github.com/DaoCloud/crproxy/tree/master/examples/simple

version: '3'
services:
  crproxy:
    image: ghcr.io/daocloud/crproxy/crproxy:v0.12.1
    container_name: crproxy
    restart: unless-stopped
    ports:
    - 80:8080
    - 443:8080
    command: |
      --acme-cache-dir=/tmp/acme
      --acme-hosts=*
      --default-registry=docker.io
    tmpfs:
      - /tmp/acme
    
    # 非必须, 如果这台服务器无法畅通的达到你要的镜像仓库可以尝试配置 
    environment:
    - https_proxy=http://proxy:8080
    - http_proxy=http://proxy:8080

由于docker被封(registry-1.docker.io/v2 被封,git pull的时候会先访问此注册表),即便配置了/etc/docker/daemon.json为自定义镜像,也无法正常拉取镜像,所以只能在镜像前指定域名拉取

如果上述服务部署的域名是: m.daocloud.io/,则这样pull:
docker pull m.daocloud.io/docker.io/library/busybox

3. cloudflare 自建worker代理docker hub (2024年9月25日实测已无法使用)

参考1参考2

  • 注意,import HTML from './docker.html'这个写法貌似不再支持,直接复制字符串塞进去就行。
//woker.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"
            }
        });
        }
    }
}
  • 记得添加自定义域名,默认分配的worker的域名被gfw封了

3. 组合拳

编辑/etc/docker/daemon.json

{
	"registry-mirrors": ["https://你的自建镜像域名","https://gcr.nju.edu.cn"]
}

重启docker即可

sudo systemctl daemon-reload 
sudo systemctl restart docker