From 0d9244bfc2e1047834bff47e3fec396dd06c85b5 Mon Sep 17 00:00:00 2001 From: MSuMshk <2039814060@qq.com> Date: Fri, 5 Dec 2025 16:35:07 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E6=95=B4=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E4=B8=8E=20Docker=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/build-adminapi.ps1 | 47 +++++++++++++++++++ .../Properties/launchSettings.json | 11 +++-- 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 scripts/build-adminapi.ps1 diff --git a/scripts/build-adminapi.ps1 b/scripts/build-adminapi.ps1 new file mode 100644 index 0000000..da00403 --- /dev/null +++ b/scripts/build-adminapi.ps1 @@ -0,0 +1,47 @@ +<# +用法:在 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 "按回车关闭窗口" diff --git a/src/Api/TakeoutSaaS.AdminApi/Properties/launchSettings.json b/src/Api/TakeoutSaaS.AdminApi/Properties/launchSettings.json index 5b8d303..20bd1cc 100644 --- a/src/Api/TakeoutSaaS.AdminApi/Properties/launchSettings.json +++ b/src/Api/TakeoutSaaS.AdminApi/Properties/launchSettings.json @@ -12,12 +12,13 @@ "Docker": { "commandName": "Docker", "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", - "publishAllPorts": true, - "useSSL": false, + "launchUrl": "http://localhost:7801/swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "publishAllPorts": false, + "useSSL": false, + "dockerRunOptions": "-p 7801:7801 --name TakeoutAdminApi" } } -} +} \ No newline at end of file