# ============================================ # Build stage with incremental cache support # Alpha最小化镜像 + 官方镜像源 # ============================================ 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-gateway/src shop-recycle-gateway/src # Build with incremental compilation (no clean!) RUN mvn package -DskipTests -B -q \ -pl shop-recycle-gateway \ -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-gateway" 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-gateway/target/*.jar gateway.jar EXPOSE 8080 # Health check (在Alpine中curl需要curl包,可用wget或java命令替代) HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1 ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar gateway.jar"]