# ============================================ # 快速 Docker 构建脚本 - PowerShell 版本 # 使用优化版 Dockerfile # 构建前请先执行:.\init-dependencies.ps1 # ============================================ param( [string]$Service = "gateway", [string]$Tag = "latest", [string]$Registry = "localhost:5000" ) $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path Set-Location $scriptDir $buildNumber = Get-Date -Format "yyyyMMddHHmmss" try { $gitCommit = git rev-parse --short HEAD } catch { $gitCommit = "unknown" } $dockerfile = "" $imageName = "" switch ($Service) { "gateway" { $dockerfile = "shop-recycle-gateway/Dockerfile.optimized" $imageName = "$Registry/shop-recycle-gateway:$Tag" } "order" { $dockerfile = "shop-recycle-order-service/Dockerfile.optimized" $imageName = "$Registry/shop-recycle-order-service:$Tag" } "payment" { $dockerfile = "shop-recycle-payment-service/Dockerfile.optimized" $imageName = "$Registry/shop-recycle-payment-service:$Tag" } default { Write-Host "未知服务: $Service" -ForegroundColor Red Write-Host "用法: .\build-docker-optimized.ps1 [gateway|order|payment] [tag] [registry]" -ForegroundColor Yellow exit 1 } } Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green Write-Host "快速构建 - 使用优化版 Dockerfile" -ForegroundColor Green Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green Write-Host "服务: $Service" -ForegroundColor Cyan Write-Host "TAG: $Tag" -ForegroundColor Cyan Write-Host "镜像: $imageName" -ForegroundColor Cyan Write-Host "Commit: $gitCommit" -ForegroundColor Cyan Write-Host "" # 检查 Dockerfile 是否存在 if (-not (Test-Path $dockerfile)) { Write-Host "❌ 错误: $dockerfile 不存在" -ForegroundColor Red Write-Host "" Write-Host "首次使用时,请先执行初始化脚本:" -ForegroundColor Yellow Write-Host " .\init-dependencies.ps1" -ForegroundColor Yellow Write-Host " .\build-docker-optimized.ps1 $Service $Tag" -ForegroundColor Yellow exit 1 } Write-Host ">>> 开始构建..." -ForegroundColor Yellow docker build ` --build-arg BUILD_NUMBER="$buildNumber" ` --build-arg GIT_COMMIT="$gitCommit" ` -t "$imageName" ` -f "$dockerfile" ` . if ($LASTEXITCODE -ne 0) { Write-Host "❌ 构建失败!" -ForegroundColor Red exit 1 } Write-Host "" Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green Write-Host "✅ 构建完成!" -ForegroundColor Green Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green Write-Host "" Write-Host "镜像详细信息:" docker images | Select-Object -First 4 Write-Host "" Write-Host "运行容器: docker run -d -p 8080:8080 $imageName" -ForegroundColor Cyan