ETest-Vue-FastAPI/fix-docker-en.ps1

198 lines
6.8 KiB
PowerShell
Raw Normal View History

2026-03-30 10:38:36 +08:00
# ETest-LIMS Docker Deployment Fix Script
# Generated by Senior DevOps Engineer
Write-Host "===== ETest-LIMS Docker Deployment Fix =====" -ForegroundColor Cyan
Write-Host "Execution Time: $(Get-Date)" -ForegroundColor Gray
Write-Host ""
# Check admin privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "[!] Please run this script as Administrator" -ForegroundColor Red
Write-Host "Right-click -> Run with PowerShell (Administrator)" -ForegroundColor Yellow
pause
exit 1
}
# 1. Check Docker status
Write-Host "[1/6] Checking Docker status..." -ForegroundColor Green
$dockerInfo = docker info 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "[!] Docker is not running, please start Docker Desktop" -ForegroundColor Red
Write-Host "1. Open Docker Desktop" -ForegroundColor Yellow
Write-Host "2. Wait for Docker to start" -ForegroundColor Yellow
Write-Host "3. Run this script again" -ForegroundColor Yellow
pause
exit 1
}
Write-Host "[OK] Docker is running" -ForegroundColor Green
# 2. Configure Docker mirror
Write-Host ""
Write-Host "[2/6] Configuring Docker mirror..." -ForegroundColor Green
$daemonConfig = @{
"registry-mirrors" = @(
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io"
)
"dns" = @("8.8.8.8", "114.114.114.114")
}
$dockerPath = "$env:USERPROFILE\.docker"
$daemonPath = "$dockerPath\daemon.json"
if (!(Test-Path $dockerPath)) {
New-Item -ItemType Directory -Path $dockerPath -Force | Out-Null
}
if (Test-Path $daemonPath) {
Copy-Item $daemonPath "$daemonPath.backup.$(Get-Date -Format 'yyyyMMddHHmmss')" -Force
Write-Host "[OK] Original config backed up" -ForegroundColor Green
}
$daemonConfig | ConvertTo-Json -Depth 10 | Out-File -FilePath $daemonPath -Encoding UTF8
Write-Host "[OK] Mirror configuration saved" -ForegroundColor Green
Write-Host " Configured mirrors:" -ForegroundColor Gray
Write-Host " - USTC Mirror" -ForegroundColor Gray
Write-Host " - NetEase Mirror" -ForegroundColor Gray
Write-Host " - Baidu Mirror" -ForegroundColor Gray
Write-Host " - DaoCloud Mirror" -ForegroundColor Gray
# 3. Clean Docker resources
Write-Host ""
Write-Host "[3/6] Cleaning Docker resources..." -ForegroundColor Green
Write-Host " Cleaning unused images and containers..." -ForegroundColor Gray
docker system prune -f 2>&1 | Out-Null
Write-Host "[OK] Cleanup completed" -ForegroundColor Green
# 4. Check port usage
Write-Host ""
Write-Host "[4/6] Checking port usage..." -ForegroundColor Green
$ports = @(3306, 6379)
$portConflict = $false
foreach ($port in $ports) {
$connection = Test-NetConnection -ComputerName localhost -Port $port -WarningAction SilentlyContinue
if ($connection.TcpTestSucceeded) {
Write-Host "[!] Port $port is in use" -ForegroundColor Yellow
$process = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue | Select-Object -First 1
if ($process) {
try {
$procInfo = Get-Process -Id $process.OwningProcess -ErrorAction SilentlyContinue
Write-Host " Process: $($procInfo.ProcessName) (PID: $($procInfo.Id))" -ForegroundColor Gray
} catch {}
}
$portConflict = $true
} else {
Write-Host "[OK] Port $port is available" -ForegroundColor Green
}
}
if ($portConflict) {
Write-Host ""
Write-Host "[!] Port conflict detected" -ForegroundColor Yellow
Write-Host "Solutions:" -ForegroundColor Yellow
Write-Host "1. Stop services using these ports" -ForegroundColor Gray
Write-Host "2. Or modify docker-compose.yml to use different ports" -ForegroundColor Gray
}
# 5. Check disk space
Write-Host ""
Write-Host "[5/6] Checking disk space..." -ForegroundColor Green
$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'"
$freeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$totalSpaceGB = [math]::Round($disk.Size / 1GB, 2)
$usedPercent = [math]::Round((($disk.Size - $disk.FreeSpace) / $disk.Size) * 100, 1)
Write-Host " C Drive: $freeSpaceGB GB / $totalSpaceGB GB (Used $usedPercent%)" -ForegroundColor Gray
if ($freeSpaceGB -lt 10) {
Write-Host "[!] Warning: Low disk space ($freeSpaceGB GB remaining)" -ForegroundColor Red
$continue = Read-Host "Continue anyway? (y/n)"
if ($continue -ne 'y') {
exit 1
}
} else {
Write-Host "[OK] Sufficient disk space" -ForegroundColor Green
}
# 6. Generate optimized docker-compose.yml
Write-Host ""
Write-Host "[6/6] Generating optimized docker-compose.yml..." -ForegroundColor Green
$projectPath = "C:\PPRO\ETest-Vue-FastAPI"
$composeContent = @"
version: '3.8'
services:
mysql:
image: mysql:8.0
container_name: etest-mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: Tgzz2025+
MYSQL_DATABASE: ruoyi-fastapi
MYSQL_USER: cpy_admin
MYSQL_PASSWORD: Tgzz2025+
TZ: Asia/Shanghai
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
networks:
- etest-network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
timeout: 20s
retries: 10
redis:
image: redis:7-alpine
container_name: etest-redis
restart: always
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: redis-server --appendonly yes
networks:
- etest-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
timeout: 20s
retries: 10
volumes:
mysql_data:
redis_data:
networks:
etest-network:
driver: bridge
"@
$composeContent | Out-File -FilePath "$projectPath\docker-compose.yml" -Encoding UTF8
Write-Host "[OK] docker-compose.yml generated" -ForegroundColor Green
# Completion message
Write-Host ""
Write-Host "===== Fix Completed =====" -ForegroundColor Cyan
Write-Host ""
Write-Host "IMPORTANT:" -ForegroundColor Yellow
Write-Host "1. Please restart Docker Desktop to apply mirror configuration" -ForegroundColor White
Write-Host "2. After restart, run: docker-compose up -d" -ForegroundColor White
Write-Host ""
Write-Host "If still cannot pull images, try:" -ForegroundColor Yellow
Write-Host "- Use mobile hotspot" -ForegroundColor Gray
Write-Host "- Or configure company proxy" -ForegroundColor Gray
Write-Host "- Or manually download and import images" -ForegroundColor Gray
Write-Host ""
Write-Host "Manual start commands:" -ForegroundColor Green
Write-Host " cd C:\PPRO\ETest-Vue-FastAPI" -ForegroundColor Gray
Write-Host " docker-compose up -d" -ForegroundColor Gray
Write-Host ""
pause