From 98b08af4a327ce4e64f325a8b8d2279029a6f6de Mon Sep 17 00:00:00 2001 From: wangtianqi <1350217033@qq.com> Date: Tue, 24 Jun 2025 09:08:13 +0800 Subject: [PATCH] fix: Remove network.host and simplify Buildx configuration Issues fixed: - Remove --network=host (not allowed by daemon config) - Remove --mount=type=cache from Dockerfile (compatibility) - Simplify builder management and error handling - Keep caches on build failure for faster retries - Use standard Docker layer caching instead of BuildKit mount This should resolve the 'granting entitlement network.host is not allowed' error. --- Dockerfile | 10 ++++------ Jenkinsfile | 21 +++++++-------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index f79b0a2..a81224f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,16 +26,14 @@ COPY mvnw . # 给mvnw脚本添加执行权限 RUN chmod +x ./mvnw -# 首先只复制pom.xml并下载依赖(利用Docker层缓存) -RUN --mount=type=cache,target=/root/.m2 \ - ./mvnw dependency:resolve -B +# 首先只下载依赖(利用Docker层缓存,不使用mount缓存) +RUN ./mvnw dependency:resolve -B -q # 复制源代码 COPY src ./src -# 构建应用(使用缓存挂载优化Maven依赖下载) -RUN --mount=type=cache,target=/root/.m2 \ - ./mvnw clean package -DskipTests -B -q +# 构建应用(简化构建过程) +RUN ./mvnw clean package -DskipTests -B -q # 运行时镜像 - 使用更小的Alpine镜像 FROM amazoncorretto:17-alpine diff --git a/Jenkinsfile b/Jenkinsfile index a09265d..dbc1713 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -155,12 +155,11 @@ pipeline { // 清理旧镜像以节省空间 sh 'docker image prune -f || true' - echo "开始构建Docker镜像: ${IMAGE_NAME}:${IMAGE_TAG}" - // 启用Docker Buildx并创建构建器 + echo "开始构建Docker镜像: ${IMAGE_NAME}:${IMAGE_TAG}" // 启用Docker Buildx并创建构建器 sh ''' echo "✅ 使用已安装的 Docker Buildx $(docker buildx version)" - # 创建并使用新的构建器实例(如果不存在) + # 简化构建器管理:重用或创建 if ! docker buildx inspect jenkins-builder >/dev/null 2>&1; then echo "创建新的构建器实例..." docker buildx create --name jenkins-builder --use --bootstrap @@ -170,22 +169,18 @@ pipeline { fi # 验证构建器状态 - docker buildx inspect --bootstrap + docker buildx inspect jenkins-builder ''' - - // 使用Buildx构建镜像,增加超时时间到30分钟 + // 使用Buildx构建镜像,增加超时时间到30分钟 timeout(time: 30, unit: 'MINUTES') { sh ''' - # 使用Buildx构建镜像,启用缓存和并行构建 + # 使用Buildx构建镜像,移除网络限制 docker buildx build \\ --builder jenkins-builder \\ --platform linux/amd64 \\ --cache-from type=local,src=/tmp/.buildx-cache \\ --cache-to type=local,dest=/tmp/.buildx-cache-new,mode=max \\ --build-arg BUILDKIT_INLINE_CACHE=1 \\ - --build-arg HTTP_PROXY= \\ - --build-arg HTTPS_PROXY= \\ - --network=host \\ --load \\ -t ${IMAGE_NAME}:${IMAGE_TAG} \\ -t ${IMAGE_NAME}:latest \\ @@ -201,13 +196,11 @@ pipeline { // 验证镜像是否创建成功 sh "docker images ${IMAGE_NAME}:${IMAGE_TAG}" - - } catch (Exception e) { + } catch (Exception e) { echo "⚠️ Docker构建失败: ${e.getMessage()}" - // 清理可能的中间状态和缓存 + // 清理可能的中间状态,但保留缓存用于下次构建 sh ''' docker system prune -f || true - rm -rf /tmp/.buildx-cache* || true ''' throw e }