flnxi-Wu/scripts/deploy/setup-docker-mirrors.ps1
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

153 lines
6.4 KiB
PowerShell

# Setup Docker mirrors that work in China
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$Password = "lxvhNYFZ4160"
)
$ErrorActionPreference = "Stop"
Write-Host "Setting up Docker mirrors for China..." -ForegroundColor Cyan
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
if (-not (Test-Path $plinkPath)) {
Write-Host "Error: plink.exe not found" -ForegroundColor Red
exit 1
}
# Chinese mirror sources that are more likely to work
$mirrors = @(
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.nju.edu.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
)
Write-Host "`n[1/4] Testing Chinese mirror sources..." -ForegroundColor Yellow
$workingMirrors = @()
foreach ($mirror in $mirrors) {
Write-Host "Testing $mirror..." -ForegroundColor Cyan
$result = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "timeout 5 curl -I $mirror/v2/ 2>&1 | head -1"
if ($result -match "200" -or $result -match "401" -or $result -match "301" -or $result -match "302") {
Write-Host " [OK] $mirror is accessible" -ForegroundColor Green
$workingMirrors += $mirror
} else {
Write-Host " [FAIL] $mirror is not accessible" -ForegroundColor Red
}
}
if ($workingMirrors.Count -eq 0) {
Write-Host "`n[WARN] No working mirrors found. Trying alternative approach..." -ForegroundColor Yellow
# Try using Aliyun container registry proxy
Write-Host "`nTrying Aliyun container registry proxy..." -ForegroundColor Cyan
$aliyunTest = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "timeout 5 curl -I https://registry.cn-hangzhou.aliyuncs.com/v2/ 2>&1 | head -1"
Write-Host "Aliyun registry test: $aliyunTest" -ForegroundColor Cyan
# Alternative: Use HTTP instead of HTTPS for some mirrors
Write-Host "`nTrying HTTP mirrors..." -ForegroundColor Cyan
$httpMirrors = @(
"http://docker.m.daocloud.io",
"http://dockerproxy.com"
)
foreach ($mirror in $httpMirrors) {
Write-Host "Testing $mirror..." -ForegroundColor Cyan
$result = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "timeout 5 curl -I $mirror/v2/ 2>&1 | head -1"
if ($result -match "200" -or $result -match "401" -or $result -match "301" -or $result -match "302") {
Write-Host " [OK] $mirror is accessible" -ForegroundColor Green
$workingMirrors += $mirror
}
}
}
if ($workingMirrors.Count -eq 0) {
Write-Host "`n[ERROR] No working mirrors found. Server may have network restrictions." -ForegroundColor Red
Write-Host "`nAlternative solutions:" -ForegroundColor Yellow
Write-Host "1. Use a proxy server" -ForegroundColor White
Write-Host "2. Export image from local machine (requires Docker Desktop running)" -ForegroundColor White
Write-Host "3. Use a VPN or different network" -ForegroundColor White
Write-Host "4. Contact server administrator to open firewall for Docker Hub" -ForegroundColor White
exit 1
}
Write-Host "`n[2/4] Found $($workingMirrors.Count) working mirror(s)" -ForegroundColor Green
$workingMirrors | ForEach-Object { Write-Host " - $_" -ForegroundColor Green }
# Update Docker daemon configuration
Write-Host "`n[3/4] Updating Docker daemon configuration..." -ForegroundColor Yellow
# Backup old config
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "cp /etc/docker/daemon.json /etc/docker/daemon.json.bak.$(date +%Y%m%d-%H%M%S) 2>/dev/null || true"
# Create new configuration
$mirrorsJson = ($workingMirrors | ForEach-Object { " `"$_`"" }) -join ",`n"
$newConfig = @"
{
"registry-mirrors": [
$mirrorsJson
],
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "3"
},
"insecure-registries": []
}
"@
# Write configuration to temp file
$tempFile = Join-Path $env:TEMP "daemon-$(Get-Date -Format 'yyyyMMdd-HHmmss').json"
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($tempFile, $newConfig, $utf8NoBom)
# Upload configuration
& $pscpPath -pw $Password -batch $tempFile "${User}@${Server}:/tmp/daemon.json"
# Move to final location
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "mv /tmp/daemon.json /etc/docker/daemon.json && chmod 644 /etc/docker/daemon.json"
Write-Host "Configuration updated" -ForegroundColor Green
# Restart Docker
Write-Host "`n[4/4] Restarting Docker..." -ForegroundColor Yellow
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "systemctl restart docker"
Write-Host "Waiting for Docker to start..." -ForegroundColor Cyan
Start-Sleep -Seconds 5
# Test pulling image
Write-Host "`nTesting image pull with new configuration..." -ForegroundColor Yellow
$pullResult = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "timeout 60 docker pull node:18-alpine 2>&1"
Write-Host $pullResult
# Cleanup
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
# Check result
if ($pullResult -match "Downloaded" -or $pullResult -match "Pulled" -or $pullResult -match "Image is up to date") {
Write-Host "`n[SUCCESS] Docker registry is now working!" -ForegroundColor Green
Write-Host "Image successfully pulled: node:18-alpine" -ForegroundColor Green
Write-Host "`nYou can now deploy your application:" -ForegroundColor Cyan
Write-Host " .\scripts\deploy\deploy-simple.ps1" -ForegroundColor Green
} elseif ($pullResult -match "already exists") {
Write-Host "`n[SUCCESS] Image already exists on server!" -ForegroundColor Green
Write-Host "You can now deploy your application:" -ForegroundColor Cyan
Write-Host " .\scripts\deploy\deploy-simple.ps1" -ForegroundColor Green
} else {
Write-Host "`n[WARN] Image pull may have failed or timed out" -ForegroundColor Yellow
Write-Host "Output: $pullResult" -ForegroundColor Yellow
Write-Host "`nYou can try:" -ForegroundColor Cyan
Write-Host "1. Check Docker logs: ssh $User@$Server 'journalctl -u docker -n 50'" -ForegroundColor White
Write-Host "2. Try pulling again manually: ssh $User@$Server 'docker pull node:18-alpine'" -ForegroundColor White
Write-Host "3. Check network connectivity from server" -ForegroundColor White
}
# Show current configuration
Write-Host "`n=== Current Docker Configuration ===" -ForegroundColor Cyan
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "cat /etc/docker/daemon.json"