ETest-Vue-FastAPI/fix-docker-and-deploy.ps1

218 lines
7.4 KiB
PowerShell

# ETest-LIMS Docker 部署修复脚本
# 由资深运维工程师生成
Write-Host "===== ETest-LIMS Docker 部署修复 =====" -ForegroundColor Cyan
Write-Host "执行时间: $(Get-Date)" -ForegroundColor Gray
Write-Host ""
# 检查管理员权限
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "[!] 请以管理员权限运行此脚本" -ForegroundColor Red
Write-Host "右键点击脚本 -> 使用 PowerShell 运行 (管理员)" -ForegroundColor Yellow
pause
exit 1
}
# 1. 检查 Docker 状态
Write-Host "[1/6] 检查 Docker 状态..." -ForegroundColor Green
$dockerInfo = docker info 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "[!] Docker 未运行,请启动 Docker Desktop" -ForegroundColor Red
Write-Host "1. 打开 Docker Desktop" -ForegroundColor Yellow
Write-Host "2. 等待 Docker 启动完成" -ForegroundColor Yellow
Write-Host "3. 重新运行此脚本" -ForegroundColor Yellow
pause
exit 1
}
Write-Host "[✓] Docker 运行正常" -ForegroundColor Green
# 2. 配置国内镜像加速
Write-Host ""
Write-Host "[2/6] 配置国内镜像加速..." -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 "[✓] 已备份原有配置" -ForegroundColor Green
}
# 写入新配置
$daemonConfig | ConvertTo-Json -Depth 10 | Out-File -FilePath $daemonPath -Encoding UTF8
Write-Host "[✓] 镜像加速配置已写入" -ForegroundColor Green
Write-Host " 配置的镜像源:" -ForegroundColor Gray
Write-Host " - 中科大镜像 (USTC)" -ForegroundColor Gray
Write-Host " - 网易云镜像 (163)" -ForegroundColor Gray
Write-Host " - 百度云镜像" -ForegroundColor Gray
Write-Host " - DaoCloud镜像" -ForegroundColor Gray
# 3. 清理 Docker 资源
Write-Host ""
Write-Host "[3/6] 清理 Docker 资源..." -ForegroundColor Green
Write-Host " 正在清理无用镜像和容器..." -ForegroundColor Gray
docker system prune -f 2>&1 | Out-Null
Write-Host "[✓] 清理完成" -ForegroundColor Green
# 4. 检查端口占用
Write-Host ""
Write-Host "[4/6] 检查端口占用..." -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 已被占用" -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 " 占用进程: $($procInfo.ProcessName) (PID: $($procInfo.Id))" -ForegroundColor Gray
} catch {}
}
$portConflict = $true
} else {
Write-Host "[✓] 端口 $port 可用" -ForegroundColor Green
}
}
if ($portConflict) {
Write-Host ""
Write-Host "[!] 检测到端口冲突" -ForegroundColor Yellow
Write-Host "解决方案:" -ForegroundColor Yellow
Write-Host "1. 停止占用端口的服务" -ForegroundColor Gray
Write-Host "2. 或修改 docker-compose.yml 使用其他端口" -ForegroundColor Gray
Write-Host " 例如: \"3307:3306\" 代替 \"3306:3306\"" -ForegroundColor Gray
}
# 5. 检查磁盘空间
Write-Host ""
Write-Host "[5/6] 检查磁盘空间..." -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盘空间: $freeSpaceGB GB / $totalSpaceGB GB (已用 $usedPercent%)" -ForegroundColor Gray
if ($freeSpaceGB -lt 10) {
Write-Host "[!] 警告: 磁盘空间不足 (剩余 $freeSpaceGB GB)" -ForegroundColor Red
Write-Host " 建议清理磁盘后再试" -ForegroundColor Yellow
$continue = Read-Host "是否继续? (y/n)"
if ($continue -ne 'y') {
exit 1
}
} else {
Write-Host "[✓] 磁盘空间充足" -ForegroundColor Green
}
# 6. 生成优化的 docker-compose.yml
Write-Host ""
Write-Host "[6/6] 生成优化的 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
- ${projectPath}/sql/init:/docker-entrypoint-initdb.d:ro
command: >
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
--innodb-buffer-pool-size=256M
--max-connections=200
networks:
- etest-network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-pTgzz2025+"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
redis:
image: redis:7-alpine
container_name: etest-redis
restart: always
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
networks:
- etest-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
volumes:
mysql_data:
driver: local
redis_data:
driver: local
networks:
etest-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
"@
$composeContent | Out-File -FilePath "$projectPath\docker-compose.yml" -Encoding UTF8
Write-Host "[✓] docker-compose.yml 已生成" -ForegroundColor Green
# 完成提示
Write-Host ""
Write-Host "===== 修复完成 =====" -ForegroundColor Cyan
Write-Host ""
Write-Host "重要提示:" -ForegroundColor Yellow
Write-Host "1. 请重启 Docker Desktop 使镜像加速配置生效" -ForegroundColor White
Write-Host "2. 重启后执行: docker-compose up -d" -ForegroundColor White
Write-Host ""
Write-Host "如果仍然无法拉取镜像,请尝试:" -ForegroundColor Yellow
Write-Host "- 使用手机热点网络" -ForegroundColor Gray
Write-Host "- 或配置公司代理" -ForegroundColor Gray
Write-Host "- 或手动下载镜像后导入" -ForegroundColor Gray
Write-Host ""
Write-Host "手动启动命令:" -ForegroundColor Green
Write-Host " cd C:\PPRO\ETest-Vue-FastAPI" -ForegroundColor Gray
Write-Host " docker-compose up -d" -ForegroundColor Gray
Write-Host ""
pause