# ============================================ # Build stage with incremental cache support # Alpine最小化镜像 + 官方镜像源 # ============================================ FROM maven:3.8-openjdk-8 AS builder ARG BUILD_NUMBER=local ARG GIT_COMMIT=unknown ARG MAVEN_OPTS=-Dmaven.wagon.http.ssl.insecure=true ENV MAVEN_OPTS="${MAVEN_OPTS}" WORKDIR /build # Copy parent pom first (if exists in multi-module project) COPY pom.xml . COPY shop-recycle-common/ shop-recycle-common/ COPY shop-recycle-gateway/pom.xml shop-recycle-gateway/ COPY shop-recycle-order-service/pom.xml shop-recycle-order-service/ COPY shop-recycle-payment-service/pom.xml shop-recycle-payment-service/ # Download dependencies (cached layer - only changes if pom.xml changes) RUN mvn dependency:go-offline -B -q # Copy sources COPY shop-recycle-order-service/src shop-recycle-order-service/src # Build with incremental compilation (no clean!) RUN mvn package -DskipTests -B -q \ -pl shop-recycle-order-service \ -am \ -Dmaven.test.skip=true \ -Ddocker.build.number=${BUILD_NUMBER} \ -Dgit.commit=${GIT_COMMIT} # ============================================ # Runtime stage - ultra-minimal image # 使用Alpine基础镜像,可进一步用distroless替代 # ============================================ FROM openjdk:8-jre-alpine LABEL maintainer="shop-recycle" LABEL service="shop-recycle-order-service" LABEL build.number=${BUILD_NUMBER} LABEL git.commit=${GIT_COMMIT} ENV TZ=Asia/Shanghai ENV JAVA_OPTS="-Xms256m -Xmx512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200" WORKDIR /app # Copy built jar from builder stage COPY --from=builder /build/shop-recycle-order-service/target/*.jar order-service.jar EXPOSE 8081 # Health check (在Alpine中wget比curl更轻量) HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:8081/health || exit 1 ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar order-service.jar"]