Files
TakeoutSaaS.TenantApi/scripts/build-adminapi-forlinux.sh

36 lines
1.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 用法:在 Linux 终端执行本脚本,自动构建并重启 AdminApi 容器。
# 前置:已安装并运行 Docker。
set -euo pipefail
# 0. 遇到异常时输出错误信息,方便查看
trap 'echo "发生错误:${BASH_COMMAND}" >&2' ERR
# 1. 基本变量(脚本位于 repo_root/scripts下移一层再上跳到仓库根
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd "${script_dir}/.." && pwd)"
image_name='takeout.api.admin:dev'
container_name='takeout.api.admin'
dockerfile_path="${repo_root}/src/Api/TakeoutSaaS.AdminApi/Dockerfile"
echo "工作目录:${repo_root}"
# 2. 停止并删除旧容器
if docker ps -a --format '{{.Names}}' | grep -qx "${container_name}"; then
echo "发现旧容器,正在移除:${container_name}"
docker stop "${container_name}" >/dev/null
docker rm "${container_name}" >/dev/null
fi
# 3. 删除旧镜像
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -qx "${image_name}"; then
echo "发现旧镜像,正在移除:${image_name}"
docker rmi "${image_name}" >/dev/null
fi
# 4. 构建最新镜像(使用仓库根作为上下文)
echo "开始构建镜像:${image_name}"
docker build -f "${dockerfile_path}" -t "${image_name}" "${repo_root}"
# 5. 运行新容器并映射端口
echo "运行新容器:${container_name} (端口映射 7801:7801环境 Development)"
docker run -d --name "${container_name}" -e ASPNETCORE_ENVIRONMENT=Development -p 7801:7801 "${image_name}"
echo "完成。镜像:${image_name},容器:${container_name}。Swagger 访问http://localhost:7801/swagger"
# 6. 交互式终端下暂停,方便查看输出
if [ -t 0 ]; then
read -r -p "按回车关闭窗口" _
fi