Docker Hub 國內拉取極慢,自建私有 Registry 解決鏡像分發效率。生產環境必須配置 HTTPS + 基礎認證。
docker run -d \
-e SETTINGS_FLAVOR=dev \
-e STORAGE_PATH=/tmp/registry \
-v /alidata/registry:/tmp/registry \
-p 5000:5000 \
--name registry \
registry:2
說明:-v 掛載宿主目錄持久化存儲,-p 映射端口。Registry 2.x 需 HTTPS,否則客戶端報錯。
# 查看倉庫鏡像列表
curl https://registry.example.com:5000/v2/_catalog
# 拉取鏡像
docker pull library/centos6
# 打標籤 (格式: 域名:端口/項目名:標籤)
docker tag 8552ea9a16f9 registry.example.com:5000/centos6:x86_64-mini
# 推送到私有倉庫
docker push registry.example.com:5000/centos6:x86_64-mini
# 安裝 httpd-tools (提供 htpasswd)
yum install -y httpd-tools
htpasswd -c /etc/nginx/docker-registry.htpasswd admin
# 輸入密碼兩次
mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
# 生成私鑰
openssl genrsa -out nginx.key 2048
# 生成 CSR
openssl req -new -key nginx.key -out nginx.csr
# Common Name 填 registry 域名
# 自簽證書 (有效期 1 年)
openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt
upstream docker-registry {
server localhost:5000;
}
server {
listen 443 ssl;
server_name registry.example.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
client_max_body_size 0; # 允許大鏡像上傳
chunked_transfer_encoding on; # 支援分塊傳輸
location / {
auth_basic "Registry Authentication";
auth_basic_user_file /etc/nginx/docker-registry.htpasswd;
proxy_pass https://docker-registry;
proxy_set_header Host $http_host;
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_read_timeout 900;
}
# 健康檢查端點無需認證
location /v2/_ping { auth_basic off; proxy_pass https://docker-registry; }
location /_ping { auth_basic off; proxy_pass https://docker-registry; }
}
# 信任自簽證書 (或配置 /etc/docker/certs.d/)
mkdir -p /etc/docker/certs.d/registry.example.com:443
cp nginx.crt /etc/docker/certs.d/registry.example.com:443/ca.crt
# 登錄
docker login https://registry.example.com
Username: admin
Password: ******
Login Succeeded
client_max_body_size、proxy_read_timeoutinsecure-registries 或導入 CA 證書docker system prune 或配置 Registry GC