既知の理由により、Docker Hub はアクセスできず、イメージのプル操作ができなくなっています。本記事はメモとして、外部イメージサーバーを使用せずに Docker Hub のイメージプル機能を解除するための設定を記録します。この記事は、読者が一般的なオペレーティングシステムのソフトウェアインストール方法(apt、yum、apk、brew など)、Nginx の基本的な設定方法、および基本的な Docker コマンドに精通していることを前提としています。イメージサイトを使用しない理由は多く、例えばイメージサイトがあなたのプルイメージの履歴を記録する可能性があります。
準備するもの#
- Nginx
- ndk_http_module
- ngx_http_lua_module
- https://www.nslookup.io/ (汚染されていない IP アドレスを取得するために使用)
Nginx 設定#
worker_processes auto;
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
events {
worker_connections 1024;
}
http {
upstream production_servers {
server 104.16.99.215:443;
server 104.16.97.215:443;
server 104.16.100.215:443;
server 104.16.98.215:443;
server 104.16.101.215:443;
}
upstream registry_servers {
server 3.94.224.37:443;
server 44.208.254.194:443;
server 98.85.153.80:443;
}
server {
listen 127.0.0.1:9999;
listen [::1]:9999;
server_name localhost;
location / {
proxy_pass https://registry_servers;
proxy_set_header Host registry-1.docker.io;
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;
proxy_ssl_verify off;
header_filter_by_lua_block {
local auth = ngx.header["www-authenticate"]
if auth then
auth = string.gsub(auth,"https://auth.docker.io", "http://localhost:9999")
ngx.header["www-authenticate"] = auth
end
}
}
location /token {
proxy_pass https://registry_servers;
proxy_set_header Host auth.docker.io;
proxy_ssl_verify off;
}
location @handle_redirect {
set $saved_redirect_location '$upstream_http_location';
if ($saved_redirect_location ~ ^(?<protocol>\w+)://(?<domain>[^/]+)(?<path>(/[^?]*)?)(?:\?(?<params>.*))?$) {
proxy_pass $protocol://production_servers$path?$params;
}
proxy_set_header Host production.cloudflare.docker.com;
proxy_ssl_verify off;
}
}
}
原理#
- SNI の除去
- Docker に内蔵されたイメージサーバーは localhost を含む
使用例#
注意:dockerhub のデフォルトイメージ組織名は library として記載されており、他のイメージは前に localhost:9999 / を追加するだけで済みます。