# ============================================ # Docker 镜像加速器配置脚本 (PowerShell) # 用于在 Windows 服务器上配置 Docker Desktop 的镜像加速器 # ============================================ param( [string]$Server = "", [string]$Username = "root", [string]$Password = "" ) $ErrorActionPreference = "Stop" Write-Host "==========================================" -ForegroundColor Cyan Write-Host "配置 Docker 镜像加速器(国内镜像源)" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan Write-Host "" # 如果提供了服务器信息,则通过 SSH 配置远程服务器 if ($Server) { Write-Host "🌐 远程服务器配置模式" -ForegroundColor Yellow Write-Host " 服务器: ${Server}" -ForegroundColor Gray Write-Host " 用户: ${Username}" -ForegroundColor Gray Write-Host "" if (-not $Password) { $Password = Read-Host "请输入SSH密码" -AsSecureString $Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password) ) } $plinkPath = "C:\Program Files\PuTTY\plink.exe" if (-not (Test-Path $plinkPath)) { Write-Host "❌ 错误: 未找到 plink.exe,请安装 PuTTY" -ForegroundColor Red exit 1 } # 检查并创建配置脚本 $scriptContent = @" #!/bin/bash set -e DOCKER_DAEMON_JSON="/etc/docker/daemon.json" MIRRORS=( "https://docker.mirrors.ustc.edu.cn" "https://hub-mirror.c.163.com" "https://mirror.baidubce.com" ) # 备份现有配置 if [ -f "`$DOCKER_DAEMON_JSON" ]; then cp "`$DOCKER_DAEMON_JSON" "`$DOCKER_DAEMON_JSON.backup.`$(date +%Y%m%d_%H%M%S)" fi # 创建配置目录 mkdir -p /etc/docker # 创建或更新配置 cat > "`$DOCKER_DAEMON_JSON" << 'EOF' { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.com", "https://mirror.baidubce.com" ] } EOF # 重启 Docker systemctl daemon-reload systemctl restart docker # 验证配置 sleep 2 docker info | grep -A 10 "Registry Mirrors" || echo "配置完成,请手动验证" "@ # 上传并执行脚本 $tempScript = [System.IO.Path]::GetTempFileName() + ".sh" $scriptContent | Out-File -FilePath $tempScript -Encoding UTF8 Write-Host "📤 上传配置脚本..." -ForegroundColor Gray $scpPath = "C:\Program Files\PuTTY\pscp.exe" if (Test-Path $scpPath) { & $scpPath -pw $Password "$tempScript" "${Username}@${Server}:/tmp/setup-docker-mirror.sh" } else { Write-Host "⚠️ 未找到 pscp.exe,将直接通过 SSH 执行命令" -ForegroundColor Yellow } Write-Host "🔧 执行配置..." -ForegroundColor Gray $command = "bash /tmp/setup-docker-mirror.sh 2>&1 || (cat > /tmp/setup-docker-mirror.sh << 'SCRIPTEND' $scriptContent SCRIPTEND chmod +x /tmp/setup-docker-mirror.sh && /tmp/setup-docker-mirror.sh)" $hostKey = "SHA256:WZOtvC/+IWw29P8vyovSabmIRFkQ/Bnmhj+qhTshA4Q" $output = & $plinkPath -batch -ssh -hostkey $hostKey -pw $Password "${Username}@${Server}" $command Write-Host "" Write-Host "📋 执行结果:" -ForegroundColor Cyan Write-Host $output # 清理临时文件 Remove-Item $tempScript -ErrorAction SilentlyContinue Write-Host "" Write-Host "✅ 远程服务器配置完成!" -ForegroundColor Green Write-Host "" Write-Host "🧪 测试镜像拉取:" -ForegroundColor Yellow Write-Host " ssh ${Username}@${Server} 'docker pull minio/minio:RELEASE.2025-04-22T22-12-26Z'" -ForegroundColor Gray } else { Write-Host "💻 本地配置模式(Windows Docker Desktop)" -ForegroundColor Yellow Write-Host "" Write-Host "⚠️ 注意: Windows Docker Desktop 需要在 GUI 中配置镜像加速器" -ForegroundColor Yellow Write-Host "" Write-Host "📝 配置步骤:" -ForegroundColor Cyan Write-Host " 1. 打开 Docker Desktop" -ForegroundColor Gray Write-Host " 2. 点击 Settings (设置)" -ForegroundColor Gray Write-Host " 3. 选择 Docker Engine" -ForegroundColor Gray Write-Host " 4. 在 JSON 配置中添加以下内容:" -ForegroundColor Gray Write-Host "" Write-Host ' {' -ForegroundColor White Write-Host ' "registry-mirrors": [' -ForegroundColor White Write-Host ' "https://docker.mirrors.ustc.edu.cn",' -ForegroundColor White Write-Host ' "https://hub-mirror.c.163.com",' -ForegroundColor White Write-Host ' "https://mirror.baidubce.com"' -ForegroundColor White Write-Host ' ]' -ForegroundColor White Write-Host ' }' -ForegroundColor White Write-Host "" Write-Host " 5. 点击 Apply & Restart" -ForegroundColor Gray Write-Host "" } Write-Host "==========================================" -ForegroundColor Cyan Write-Host "配置说明完成" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan