diff --git a/modify.md b/modify.md index a2232349..5046a941 100644 --- a/modify.md +++ b/modify.md @@ -73,6 +73,12 @@ - 构建时使用 `--no-cache` 选项,确保不使用缓存的镜像层 - 解决因缓存导致的构建问题,确保每次部署都是全新构建 +- **修复 dangling 镜像问题**: + - 在构建前删除同名的旧镜像,避免构建新镜像时产生 `` 标签的 dangling 镜像 + - 构建完成后再次清理 dangling 镜像 + - 自动识别项目名称(如 `site`)并匹配对应的镜像名称(如 `site-marketing-site`) + - 解决 `docker images` 中出现 `` 镜像的问题 + ## 2025-01-XX - 修复 Next.js 配置警告 ### 修复内容 diff --git a/site/deploy-https.sh b/site/deploy-https.sh index fc4e8daf..ed838f5f 100644 --- a/site/deploy-https.sh +++ b/site/deploy-https.sh @@ -70,10 +70,26 @@ echo "" echo "Step 3: Cleaning up Docker images and build cache..." echo "----------------------------------------" -# Remove dangling images (untagged images) +# Remove dangling images (untagged images) before build echo "Removing dangling images..." docker image prune -f || true +# Get project name from directory name or docker-compose +PROJECT_NAME=$(basename "$SCRIPT_DIR" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]//g') +if [ -z "$PROJECT_NAME" ] || [ "$PROJECT_NAME" = "site" ]; then + PROJECT_NAME="site" +fi + +# Try to remove old images with same name to avoid creating dangling images +# Docker Compose creates images with format: _ +echo "Removing old images: ${PROJECT_NAME}_marketing-site" +docker images --format "{{.Repository}}:{{.Tag}}" | grep -E "^${PROJECT_NAME}_marketing-site" | while read image; do + if [ ! -z "$image" ]; then + echo "Removing old image: $image" + docker rmi "$image" 2>/dev/null || true + fi +done || true + # Remove build cache to ensure fresh build echo "Removing build cache..." docker builder prune -f || true @@ -87,6 +103,14 @@ echo "----------------------------------------" $DOCKER_COMPOSE -f docker-compose-https.yml build --no-cache $DOCKER_COMPOSE -f docker-compose-https.yml up -d +echo "" +echo "Step 4.5: Cleaning up dangling images after build..." +echo "----------------------------------------" + +# Remove dangling images again after build (new images may have replaced old ones) +echo "Removing dangling images created during build..." +docker image prune -f || true + echo "" echo "Step 5: Checking container status..." echo "----------------------------------------"