diff --git a/archive-manager.sh b/archive-manager.sh index bd55164..efea7d2 100755 --- a/archive-manager.sh +++ b/archive-manager.sh @@ -117,6 +117,12 @@ check_scripts() { done } +# 镜像是否存在 +image_exists() { + local image_ref="$1" + docker image inspect "$image_ref" >/dev/null 2>&1 +} + # 构建镜像 build_image() { log_info "开始构建Docker镜像..." @@ -183,10 +189,13 @@ deploy_app() { log_info "部署应用到: $deploy_dir" - # 检查镜像 - if ! docker images | grep -q "${PROJECT_NAME}:${VERSION}"; then - log_error "镜像 ${PROJECT_NAME}:${VERSION} 不存在,请先构建: $0 build" - exit 1 + # 检查镜像(若不存在则自动构建一次) + if ! image_exists "${PROJECT_NAME}:${VERSION}"; then + log_warn "未找到镜像 ${PROJECT_NAME}:${VERSION},尝试自动构建..." + build_image || { + log_error "自动构建镜像失败,请手动执行: $0 build"; + exit 1; + } fi # 创建部署目录 diff --git a/operations-log.md b/operations-log.md index 3e2f38e..166d53a 100644 --- a/operations-log.md +++ b/operations-log.md @@ -7,3 +7,11 @@ - 目的:修复构建阶段报错“Option g is ambiguous (gecos, gid, group)”,避免不同基础镜像工具链参数差异导致失败。 - 工具:apply_patch(补丁写入) - 结果:补丁应用成功,建议以 `docker build --no-cache` 重新构建验证。 + +- 时间:2025-11-01 19:40(UTC+8) +- 动作:修复部署脚本 archive-manager.sh 镜像检查逻辑 +- 位置:archive-manager.sh(deploy_app 函数) +- 变更:新增 `image_exists` 检查,通过 `docker image inspect` 判断本地镜像是否存在;若不存在,`deploy` 流程自动调用 `build_image` 进行构建,避免 `docker compose up` 因尝试拉取远端镜像而失败(镜像找不到)。 +- 目的:解决部署时报“镜像找不到”的问题,提升一键部署体验。 +- 工具:apply_patch(补丁写入) +- 结果:补丁应用成功,可通过 `./archive-manager.sh deploy <目录>` 直接部署,首次会自动构建镜像。