Docker Hub 因为从所周知的原因在中国被禁用了,并且之前用到的镜像仓库也都不能用了。对于经常使用Docker的我们来说,会非常不方便。

这里提供一分我自己在用的自己解决方案:

代理:因为Docker的镜像仓库是基于Http协议的,因此我们也可以通过在海外服务器上部署反向代理设施,比如Nginx来直接代理DockerHub。

 

server
{
    listen      80;
    listen 443 ssl;
    server_name aaa.bbb.com; #修改成自己域名
    index index.html index.htm;
    root  /data/www/blog/;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Headers' 'userTicket,ssotoken,token,Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    send_timeout 600;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    proxy_buffer_size 64k;
    proxy_buffers 8 64k;
    ssl_certificate conf.d/cert/XXX.pem;   ###替换自己的证书和 key
    ssl_certificate_key conf.d/cert/XXX.key; ###替换自己的证书和 key
    
 location / {
                     proxy_pass https://registry-1.docker.io;  
                     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;
                     proxy_intercept_errors on;
                     recursive_error_pages on;
                     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;
             }

    access_log  /data/www/logs/nginx/docker.access.log;
    error_log  /data/www/logs/nginx/docker.error.log;

}