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

83 lines
3.0 KiB
PowerShell

# Setup Docker mirror for server
# This script configures the Docker daemon to use the working mirror: https://docker.m.daocloud.io
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$Password = "lxvhNYFZ4160"
)
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Setup Docker Mirror: docker.m.daocloud.io" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
if (-not (Test-Path $plinkPath) -or -not (Test-Path $pscpPath)) {
Write-Host "Error: plink.exe or pscp.exe not found" -ForegroundColor Red
exit 1
}
# Create daemon.json with the working mirror
$daemonJson = @"
{
"registry-mirrors": ["https://docker.m.daocloud.io"]
}
"@
$tempDaemonJson = Join-Path $env:TEMP "daemon-$(Get-Date -Format 'yyyyMMdd-HHmmss').json"
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
$bytes = $utf8NoBom.GetBytes($daemonJson)
[System.IO.File]::WriteAllBytes($tempDaemonJson, $bytes)
Write-Host "1. Uploading daemon.json..." -ForegroundColor Yellow
$pscpCmd = "`"$pscpPath`" -pw `"$Password`" -batch `"$tempDaemonJson`" ${User}@${Server}:/tmp/daemon.json"
$uploadResult = cmd /c $pscpCmd 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host " Failed to upload daemon.json" -ForegroundColor Red
Remove-Item $tempDaemonJson -Force -ErrorAction SilentlyContinue
exit 1
}
Write-Host " Upload successful" -ForegroundColor Green
Write-Host ""
Write-Host "2. Applying configuration..." -ForegroundColor Yellow
$applyCmd = "sudo cp /tmp/daemon.json /etc/docker/daemon.json && sudo systemctl restart docker && sleep 3 && echo 'Docker restarted successfully'"
$plinkCmd = "`"$plinkPath`" -ssh -pw `"$Password`" -batch ${User}@${Server} `"$applyCmd`""
$applyResult = cmd /c $plinkCmd 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " Configuration applied successfully" -ForegroundColor Green
Write-Host $applyResult
} else {
Write-Host " Failed to apply configuration" -ForegroundColor Red
Write-Host $applyResult -ForegroundColor Red
Remove-Item $tempDaemonJson -Force -ErrorAction SilentlyContinue
exit 1
}
Write-Host ""
Write-Host "3. Verifying configuration..." -ForegroundColor Yellow
$verifyCmd = "cat /etc/docker/daemon.json"
$plinkCmd = "`"$plinkPath`" -ssh -pw `"$Password`" -batch ${User}@${Server} `"$verifyCmd`""
$verifyResult = cmd /c $plinkCmd 2>&1
Write-Host $verifyResult
Write-Host ""
# Cleanup
Remove-Item $tempDaemonJson -Force -ErrorAction SilentlyContinue
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Docker mirror configured successfully!" -ForegroundColor Green
Write-Host "Mirror: https://docker.m.daocloud.io" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "This configuration will be used for all future Docker operations." -ForegroundColor Yellow
Write-Host ""