| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- # ============================================
- # Docker镜像构建脚本 (PowerShell版本)
- # 支持国内多个镜像源,自动选择最优方案
- # ============================================
- param(
- [string]$Service = "gateway",
- [string]$Tag = "latest",
- [string]$Registry = "aliyun"
- )
- # 颜色定义
- $Green = "Green"
- $Yellow = "Yellow"
- $Red = "Red"
- # 镜像源映射
- $Registries = @{
- aliyun = "registry.aliyuncs.com/library"
- netease = "hub.c.163.com/library"
- tsinghua = "docker.mirrors.tsinghua.edu.cn/library"
- ustc = "docker.mirrors.ustc.edu.cn/library"
- }
- # 服务映射
- $Services = @{
- gateway = "gateway"
- order = "order-service"
- payment = "payment-service"
- }
- # ============================================
- # 函数定义
- # ============================================
- function Print-Usage {
- Write-Host @"
- 使用方法: .\build-docker.ps1 [-Service] [-Tag] [-Registry]
- 参数:
- Service - 服务名称 (gateway|order|payment) [默认: gateway]
- Tag - 镜像标签 [默认: latest]
- Registry - 镜像源 (aliyun|netease|tsinghua|ustc|auto) [默认: aliyun]
- 示例:
- # 构建gateway镜像,使用阿里云源
- .\build-docker.ps1 -Service gateway -Tag latest -Registry aliyun
- # 构建order-service镜像,使用网易云源
- .\build-docker.ps1 -Service order -Tag 1.0.0 -Registry netease
- # 智能选择镜像源(自动检测最快源)
- .\build-docker.ps1 -Registry auto
- # 快速命令(使用位置参数)
- .\build-docker.ps1 gateway latest aliyun
- .\build-docker.ps1 order 1.0.0 netease
- "@
- }
- function Check-Registry {
- param([string]$Registry)
-
- Write-Host "检查镜像源: $Registry" -ForegroundColor Yellow
-
- try {
- $response = Invoke-WebRequest -Uri "https://$Registry/library/maven" `
- -Method Head `
- -TimeoutSec 5 `
- -ErrorAction SilentlyContinue
- Write-Host "✓ $Registry 可用" -ForegroundColor Green
- return $true
- } catch {
- Write-Host "✗ $Registry 不可用" -ForegroundColor Red
- return $false
- }
- }
- function Select-BestRegistry {
- Write-Host "开始检测可用镜像源..." -ForegroundColor Yellow
-
- $registryList = @("aliyun", "netease", "tsinghua", "ustc")
-
- foreach ($reg in $registryList) {
- if (Check-Registry $Registries[$reg]) {
- Write-Host "选择镜像源: $reg" -ForegroundColor Green
- return $reg
- }
- }
-
- Write-Host "所有镜像源都不可用,使用默认的阿里云" -ForegroundColor Red
- return "aliyun"
- }
- function Build-Image {
- param(
- [string]$Service,
- [string]$Tag,
- [string]$Registry
- )
-
- $registryUrl = $Registries[$Registry]
- $serviceDir = "shop-recycle-$Service"
-
- if (-not (Test-Path $serviceDir)) {
- Write-Host "错误: 目录 $serviceDir 不存在" -ForegroundColor Red
- exit 1
- }
-
- Write-Host "开始构建镜像:" -ForegroundColor Yellow
- Write-Host " 服务: $Service"
- Write-Host " 标签: $Tag"
- Write-Host " 镜像源: $Registry ($registryUrl)"
- Write-Host ""
-
- $dockerfile = "$serviceDir/Dockerfile"
-
- # 执行Docker构建
- $buildArgs = @(
- "build",
- "--build-arg", "REGISTRY=$registryUrl",
- "--build-arg", "BUILD_NUMBER=$Tag",
- "--cache-from", "shop-recycle/$Service`:latest",
- "-t", "shop-recycle/$Service`:$Tag",
- "-t", "shop-recycle/$Service`:latest",
- "-f", $dockerfile,
- "."
- )
-
- & docker $buildArgs
-
- if ($LASTEXITCODE -ne 0) {
- Write-Host "错误: 镜像构建失败" -ForegroundColor Red
- exit 1
- }
-
- Write-Host "✓ 镜像构建完成" -ForegroundColor Green
-
- # 显示镜像信息
- Write-Host ""
- Write-Host "镜像信息:" -ForegroundColor Yellow
- & docker images | Select-String "shop-recycle/$Service" | Select-Object -First 2
- }
- # ============================================
- # 主程序
- # ============================================
- # 帮助参数
- if ($Service -eq "--help" -or $Service -eq "-h") {
- Print-Usage
- exit 0
- }
- # 验证SERVICE参数
- if (-not $Services.ContainsKey($Service)) {
- Write-Host "错误: 未知的服务 '$Service'" -ForegroundColor Red
- Print-Usage
- exit 1
- }
- # 处理镜像源参数
- if ($Registry -eq "auto") {
- $Registry = Select-BestRegistry
- } elseif (-not $Registries.ContainsKey($Registry)) {
- Write-Host "错误: 未知的镜像源 '$Registry'" -ForegroundColor Red
- exit 1
- }
- # 执行构建
- Build-Image -Service $Service -Tag $Tag -Registry $Registry
- Write-Host ""
- Write-Host "====================================" -ForegroundColor Green
- Write-Host "构建成功!" -ForegroundColor Green
- Write-Host "镜像: shop-recycle/$Service`:$Tag" -ForegroundColor Green
- Write-Host "====================================" -ForegroundColor Green
- Write-Host ""
- Write-Host "下一步:" -ForegroundColor Yellow
- Write-Host " # 推送镜像"
- Write-Host " docker push shop-recycle/$Service`:$Tag"
- Write-Host ""
- Write-Host " # 运行容器"
- Write-Host " docker run -d -p 8080:8080 shop-recycle/$Service`:$Tag"
- Write-Host ""
- Write-Host " # 查看日志"
- Write-Host " docker logs -f \$(docker ps -q -f ancestor=shop-recycle/$Service`:$Tag)"
|