Spring Boot 分层 jar + docker builder

时间:2021-05-12 19:38:18

标签: spring spring-boot docker maven jar

我的目标是使用分层 jar 设置多阶段 docker 镜像构建。

为此,我使用了以下 Dockerfile:

FROM maven:3.6-openjdk-16-slim AS builder
WORKDIR application

COPY pom.xml ./
# store maven dependencies so next build doesn't have to download them again
RUN mvn dependency:go-offline

COPY src src
RUN mvn package

# LAYERED JAR
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} ./application.jar
RUN java -Djarmode=layertools -jar application.jar extract

# Alpine Linux with OpenJDK JRE
FROM azul/zulu-openjdk-alpine:16-jre
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./

#JVM
ENV MAX_HEAP_MB="512"

# Appliction Insights
ENV APP_INSIGHTS_JAR_FILE=app-insights-agent.jar

RUN wget --no-verbose --no-check-certificate -O ./${APP_INSIGHTS_JAR_FILE} https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.0.3/applicationinsights-agent-3.0.3.jar
ENTRYPOINT java -javaagent:./${APP_INSIGHTS_JAR_FILE} -Xmx${MAX_HEAP_MB}m org.springframework.boot.loader.JarLauncher

乍一看,这很有魅力,问题在于您何时想要“重建”您的形象。为该 dockerfile 运行 docker build 会生成一个没有最新代码更改的图像。
clean 添加到 mvn 包解决了问题,但我无法缓存 mvn dependency:go-offline 给我的依赖项,因为所有依赖项都重新下载。
我已经尝试添加

RUN rm -rf target/classes

但没有效果...

谢谢

编辑: 解决办法是改

COPY ${JAR_FILE} ./application.jar

为了

RUN cp ${JAR_FILE} application.jar

0 个答案:

没有答案