build-docker.ps1 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # ============================================
  2. # Docker镜像构建脚本 (PowerShell版本)
  3. # 支持国内多个镜像源,自动选择最优方案
  4. # ============================================
  5. param(
  6. [string]$Service = "gateway",
  7. [string]$Tag = "latest",
  8. [string]$Registry = "aliyun"
  9. )
  10. # 颜色定义
  11. $Green = "Green"
  12. $Yellow = "Yellow"
  13. $Red = "Red"
  14. # 镜像源映射
  15. $Registries = @{
  16. aliyun = "registry.aliyuncs.com/library"
  17. netease = "hub.c.163.com/library"
  18. tsinghua = "docker.mirrors.tsinghua.edu.cn/library"
  19. ustc = "docker.mirrors.ustc.edu.cn/library"
  20. }
  21. # 服务映射
  22. $Services = @{
  23. gateway = "gateway"
  24. order = "order-service"
  25. payment = "payment-service"
  26. }
  27. # ============================================
  28. # 函数定义
  29. # ============================================
  30. function Print-Usage {
  31. Write-Host @"
  32. 使用方法: .\build-docker.ps1 [-Service] [-Tag] [-Registry]
  33. 参数:
  34. Service - 服务名称 (gateway|order|payment) [默认: gateway]
  35. Tag - 镜像标签 [默认: latest]
  36. Registry - 镜像源 (aliyun|netease|tsinghua|ustc|auto) [默认: aliyun]
  37. 示例:
  38. # 构建gateway镜像,使用阿里云源
  39. .\build-docker.ps1 -Service gateway -Tag latest -Registry aliyun
  40. # 构建order-service镜像,使用网易云源
  41. .\build-docker.ps1 -Service order -Tag 1.0.0 -Registry netease
  42. # 智能选择镜像源(自动检测最快源)
  43. .\build-docker.ps1 -Registry auto
  44. # 快速命令(使用位置参数)
  45. .\build-docker.ps1 gateway latest aliyun
  46. .\build-docker.ps1 order 1.0.0 netease
  47. "@
  48. }
  49. function Check-Registry {
  50. param([string]$Registry)
  51. Write-Host "检查镜像源: $Registry" -ForegroundColor Yellow
  52. try {
  53. $response = Invoke-WebRequest -Uri "https://$Registry/library/maven" `
  54. -Method Head `
  55. -TimeoutSec 5 `
  56. -ErrorAction SilentlyContinue
  57. Write-Host "✓ $Registry 可用" -ForegroundColor Green
  58. return $true
  59. } catch {
  60. Write-Host "✗ $Registry 不可用" -ForegroundColor Red
  61. return $false
  62. }
  63. }
  64. function Select-BestRegistry {
  65. Write-Host "开始检测可用镜像源..." -ForegroundColor Yellow
  66. $registryList = @("aliyun", "netease", "tsinghua", "ustc")
  67. foreach ($reg in $registryList) {
  68. if (Check-Registry $Registries[$reg]) {
  69. Write-Host "选择镜像源: $reg" -ForegroundColor Green
  70. return $reg
  71. }
  72. }
  73. Write-Host "所有镜像源都不可用,使用默认的阿里云" -ForegroundColor Red
  74. return "aliyun"
  75. }
  76. function Build-Image {
  77. param(
  78. [string]$Service,
  79. [string]$Tag,
  80. [string]$Registry
  81. )
  82. $registryUrl = $Registries[$Registry]
  83. $serviceDir = "shop-recycle-$Service"
  84. if (-not (Test-Path $serviceDir)) {
  85. Write-Host "错误: 目录 $serviceDir 不存在" -ForegroundColor Red
  86. exit 1
  87. }
  88. Write-Host "开始构建镜像:" -ForegroundColor Yellow
  89. Write-Host " 服务: $Service"
  90. Write-Host " 标签: $Tag"
  91. Write-Host " 镜像源: $Registry ($registryUrl)"
  92. Write-Host ""
  93. $dockerfile = "$serviceDir/Dockerfile"
  94. # 执行Docker构建
  95. $buildArgs = @(
  96. "build",
  97. "--build-arg", "REGISTRY=$registryUrl",
  98. "--build-arg", "BUILD_NUMBER=$Tag",
  99. "--cache-from", "shop-recycle/$Service`:latest",
  100. "-t", "shop-recycle/$Service`:$Tag",
  101. "-t", "shop-recycle/$Service`:latest",
  102. "-f", $dockerfile,
  103. "."
  104. )
  105. & docker $buildArgs
  106. if ($LASTEXITCODE -ne 0) {
  107. Write-Host "错误: 镜像构建失败" -ForegroundColor Red
  108. exit 1
  109. }
  110. Write-Host "✓ 镜像构建完成" -ForegroundColor Green
  111. # 显示镜像信息
  112. Write-Host ""
  113. Write-Host "镜像信息:" -ForegroundColor Yellow
  114. & docker images | Select-String "shop-recycle/$Service" | Select-Object -First 2
  115. }
  116. # ============================================
  117. # 主程序
  118. # ============================================
  119. # 帮助参数
  120. if ($Service -eq "--help" -or $Service -eq "-h") {
  121. Print-Usage
  122. exit 0
  123. }
  124. # 验证SERVICE参数
  125. if (-not $Services.ContainsKey($Service)) {
  126. Write-Host "错误: 未知的服务 '$Service'" -ForegroundColor Red
  127. Print-Usage
  128. exit 1
  129. }
  130. # 处理镜像源参数
  131. if ($Registry -eq "auto") {
  132. $Registry = Select-BestRegistry
  133. } elseif (-not $Registries.ContainsKey($Registry)) {
  134. Write-Host "错误: 未知的镜像源 '$Registry'" -ForegroundColor Red
  135. exit 1
  136. }
  137. # 执行构建
  138. Build-Image -Service $Service -Tag $Tag -Registry $Registry
  139. Write-Host ""
  140. Write-Host "====================================" -ForegroundColor Green
  141. Write-Host "构建成功!" -ForegroundColor Green
  142. Write-Host "镜像: shop-recycle/$Service`:$Tag" -ForegroundColor Green
  143. Write-Host "====================================" -ForegroundColor Green
  144. Write-Host ""
  145. Write-Host "下一步:" -ForegroundColor Yellow
  146. Write-Host " # 推送镜像"
  147. Write-Host " docker push shop-recycle/$Service`:$Tag"
  148. Write-Host ""
  149. Write-Host " # 运行容器"
  150. Write-Host " docker run -d -p 8080:8080 shop-recycle/$Service`:$Tag"
  151. Write-Host ""
  152. Write-Host " # 查看日志"
  153. Write-Host " docker logs -f \$(docker ps -q -f ancestor=shop-recycle/$Service`:$Tag)"