<# 用法:在 PowerShell 中执行本脚本,自动构建并重启 AdminApi 容器。 如果要放到桌面双击运行,可将本文件复制到桌面后右键“使用 PowerShell 运行”。 前置:已安装并运行 Docker Desktop。 #> # 遇到异常时停住窗口,方便查看错误 trap { Write-Host "发生错误:" $_ -ForegroundColor Red Read-Host "按回车关闭窗口" exit 1 } $ErrorActionPreference = 'Stop' # 1. 基本变量(脚本位于 repo_root/scripts,下移一层再上跳到仓库根) $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $repoRoot = Split-Path -Parent $scriptDir $imageName = 'takeout.api.admin:dev' $containerName = 'takeout.api.admin' $dockerfilePath = Join-Path $repoRoot 'src/Api/TakeoutSaaS.AdminApi/Dockerfile' Write-Host "工作目录:$repoRoot" # 2. 停止并删除旧容器 if ((docker ps -a --format '{{.Names}}') -contains $containerName) { Write-Host "发现旧容器,正在移除:$containerName" docker stop $containerName | Out-Null docker rm $containerName | Out-Null } # 3. 删除旧镜像 if ((docker images --format '{{.Repository}}:{{.Tag}}') -contains $imageName) { Write-Host "发现旧镜像,正在移除:$imageName" docker rmi $imageName | Out-Null } # 4. 构建最新镜像(使用仓库根作为上下文) Write-Host "开始构建镜像:$imageName" docker build -f $dockerfilePath -t $imageName $repoRoot # 5. 运行新容器并映射端口 Write-Host "运行新容器:$containerName (端口映射 7801:7801,环境 Development)" docker run -d --name $containerName -e ASPNETCORE_ENVIRONMENT=Development -p 7801:7801 $imageName Write-Host "完成。镜像:$imageName,容器:$containerName。Swagger 访问:http://localhost:7801/swagger" Read-Host "按回车关闭窗口"