Docker hub镜像国内加速原理和nginx配置示例

在使用 Docker 拉取镜像时,有时会遇到速度慢或无法下载的情况,配置 Docker 镜像加速服务可以有效解决这些问题,以下是相关的docker镜像加速原理和nginx docker镜像加速的配置方法:

docker镜像加速原理:

docker镜像的加速原理为,每次拉取镜像的时候docker都会发送3个请求,他们分别是

GET /v2/

GET /token

HEAD /v2/

如果你想要配置一个自己的docker镜像加速服务的话,只需要再你的服务器上面对增加上面3个请求的响应配置即可。

这3个请求的示例如下:

bash 复制代码
"GET /v2/ HTTP/1.1" 401 87 "-" "docker/20.10.21 go/go1.18.7 git-commit/3056208 kernel/5.15.49-linuxkit os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.21 \x5C(darwin\x5C))" 

"GET /token?scope=repository%3Atekintian%2Falpine-mariadb%3Apull&service=registry.docker.io HTTP/1.1" 200 5450 "-" "docker/20.10.21 go/go1.18.7 git-commit/3056208 kernel/5.15.49-linuxkit os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.21 \x5C(darwin\x5C))"

"HEAD /v2/tekintian/alpine-mariadb/manifests/10.11 HTTP/1.1" 200 0 "-" "docker/20.10.21 go/go1.18.7 git-commit/3056208 kernel/5.15.49-linuxkit os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.21 \x5C(darwin\x5C))" 

从上面的请求示例中可以看出,第一个GET请求是切断服务是否可用,第二个是获取授权信息, 第三个是获取镜像头信息。

nginx docker镜像加速配置示例

Matlab 复制代码
server {
    listen 80 ;
    listen 443 ssl ;
    server_name hub.youname.com;
    index index.html index.htm default.htm default.html;
    access_log /www/hub.youname.com/log/access.log main;
    error_log /www/hub.youname.com/log/error.log;

    # ssl证书配置
    ssl_certificate /www/hub.youname.com/ssl/fullchain.pem;
    ssl_certificate_key /www/hub.youname.com/ssl/privkey.pem;
    ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1 TLSv1;
    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:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:!aNULL:!eNULL:!EXPORT:!DSS:!DES:!RC4:!3DES:!MD5:!PSK:!KRB5:!SRP:!CAMELLIA:!SEED;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497 https://$host$request_uri;
    proxy_set_header X-Forwarded-Proto https;
    add_header Strict-Transport-Security "max-age=31536000";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    # 重定向http到https
    if ($scheme = http) {
        return 301 https://$host$request_uri;
    }
    # docker镜像加速配置
    location /v2/ {
        proxy_pass https://registry-1.docker.io;
        proxy_set_header Host registry-1.docker.io;
        # 这段是lua的auth配置,如果你的nginx不支持lua,删除即可
        header_filter_by_lua_block {
            local www_auth = ngx.var.upstream_http_www_authenticate
            if www_auth then
                local new_www_auth = string.gsub(www_auth, "auth.docker.io", "hub.youname.com")
                ngx.header['www-authenticate'] = new_www_auth
            end
        }
        # lua auth conf end

        # 异常处理
        proxy_intercept_errors on;
        recursive_error_pages on;
        error_page 301 302 307 = @handle_redirect;
    }

    location /token {
        proxy_pass https://auth.docker.io;
        proxy_set_header Host auth.docker.io;
    }
    # 异常处理 配置
    location @handle_redirect {
        set $saved_redirect_location '$upstream_http_location';
        proxy_pass $saved_redirect_location;
    }


}

测试你自己配置的加速镜像

经过上面的一番配置后你的镜像应该就可以使用了, 你可以通过这个url地址https://hub.youname.com/v2/tekintian/alpine-mariadb/manifests/latest来查看是否正常访问, 如果出现如下界面,表明你的docker镜像加速服务部署成功了。

在Docker里面使用你自己的加速镜像地址

打开你的docker,在Preferences --> Docker Engine 里面的 "registry-mirrors" 节点增加你的加速地址即可, 如下图所示:

这里的配置实际上就是对应的 daemon.json的配置, 在你自己的daemon.json配置的registry-mirrors里面增加也是一样的效果。

XML 复制代码
{
  "dns": [
    "8.8.8.8",
    "8.8.4.4"
  ],
  "registry-mirrors": [
    "https://ustc-edu-cn.mirror.aliyuncs.com",
    "https://docker.nju.edu.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub.youname.com"
  ]
}
相关推荐
IvanCodes1 分钟前
一、Docker:一场颠覆应用部署与运维的容器革命
docker·容器
栗子~~16 分钟前
Milvus docker-compose 部署
docker·容器·milvus
椰汁菠萝1 小时前
ubuntu下免sudo执行docker
ubuntu·docker·免sudo
紫璨月2 小时前
nginx反向代理的bug
运维·nginx·bug
没有名字的小羊2 小时前
2.安装Docker
运维·docker·容器
xiezhr2 小时前
50 个常用 Docker 命令
运维·docker·容器
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
小生云木10 天前
Linux离线编译安装nginx
linux·运维·nginx
API开发10 天前
苹果芯片macOS安装版Homebrew(亲测) ,一键安装node、python、vscode等,比绿色软件还干净、无污染
vscode·python·docker·nodejs·openssl·brew·homebrew
进击的码码码码N10 天前
Docker 镜像加速
运维·docker·容器