build-docker-optimized.ps1 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # ============================================
  2. # 快速 Docker 构建脚本 - PowerShell 版本
  3. # 使用优化版 Dockerfile
  4. # 构建前请先执行:.\init-dependencies.ps1
  5. # ============================================
  6. param(
  7. [string]$Service = "gateway",
  8. [string]$Tag = "latest",
  9. [string]$Registry = "localhost:5000"
  10. )
  11. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  12. Set-Location $scriptDir
  13. $buildNumber = Get-Date -Format "yyyyMMddHHmmss"
  14. try {
  15. $gitCommit = git rev-parse --short HEAD
  16. } catch {
  17. $gitCommit = "unknown"
  18. }
  19. $dockerfile = ""
  20. $imageName = ""
  21. switch ($Service) {
  22. "gateway" {
  23. $dockerfile = "shop-recycle-gateway/Dockerfile.optimized"
  24. $imageName = "$Registry/shop-recycle-gateway:$Tag"
  25. }
  26. "order" {
  27. $dockerfile = "shop-recycle-order-service/Dockerfile.optimized"
  28. $imageName = "$Registry/shop-recycle-order-service:$Tag"
  29. }
  30. "payment" {
  31. $dockerfile = "shop-recycle-payment-service/Dockerfile.optimized"
  32. $imageName = "$Registry/shop-recycle-payment-service:$Tag"
  33. }
  34. default {
  35. Write-Host "未知服务: $Service" -ForegroundColor Red
  36. Write-Host "用法: .\build-docker-optimized.ps1 [gateway|order|payment] [tag] [registry]" -ForegroundColor Yellow
  37. exit 1
  38. }
  39. }
  40. Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
  41. Write-Host "快速构建 - 使用优化版 Dockerfile" -ForegroundColor Green
  42. Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
  43. Write-Host "服务: $Service" -ForegroundColor Cyan
  44. Write-Host "TAG: $Tag" -ForegroundColor Cyan
  45. Write-Host "镜像: $imageName" -ForegroundColor Cyan
  46. Write-Host "Commit: $gitCommit" -ForegroundColor Cyan
  47. Write-Host ""
  48. # 检查 Dockerfile 是否存在
  49. if (-not (Test-Path $dockerfile)) {
  50. Write-Host "❌ 错误: $dockerfile 不存在" -ForegroundColor Red
  51. Write-Host ""
  52. Write-Host "首次使用时,请先执行初始化脚本:" -ForegroundColor Yellow
  53. Write-Host " .\init-dependencies.ps1" -ForegroundColor Yellow
  54. Write-Host " .\build-docker-optimized.ps1 $Service $Tag" -ForegroundColor Yellow
  55. exit 1
  56. }
  57. Write-Host ">>> 开始构建..." -ForegroundColor Yellow
  58. docker build `
  59. --build-arg BUILD_NUMBER="$buildNumber" `
  60. --build-arg GIT_COMMIT="$gitCommit" `
  61. -t "$imageName" `
  62. -f "$dockerfile" `
  63. .
  64. if ($LASTEXITCODE -ne 0) {
  65. Write-Host "❌ 构建失败!" -ForegroundColor Red
  66. exit 1
  67. }
  68. Write-Host ""
  69. Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
  70. Write-Host "✅ 构建完成!" -ForegroundColor Green
  71. Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
  72. Write-Host ""
  73. Write-Host "镜像详细信息:"
  74. docker images | Select-Object -First 4
  75. Write-Host ""
  76. Write-Host "运行容器: docker run -d -p 8080:8080 $imageName" -ForegroundColor Cyan