Dockerfile

虽然可以将 Spring Boot 的 uber jar 转换成 Docker 镜像,只需在Dockerfile使用分层功能将得到优化图像。 当你创建一个包含图层索引文件的jar时,Spring-boot-jarmode-toolsjar 会作为依赖添加到你的 jar 中。 有了这个 jar 在类路径上,你可以以特殊模式启动应用,允许引导代码运行与应用完全不同的程序,比如提取层的程序。spring-doc.cadn.net.cn

以下是你可以用工具罐模式:spring-doc.cadn.net.cn

$ java -Djarmode=tools -jar my-app.jar

这将得到以下输出:spring-doc.cadn.net.cn

Usage:
  java -Djarmode=tools -jar my-app.jar

Available commands:
  extract      Extract the contents from the jar
  list-layers  List layers from the jar that can be extracted
  help         Help about any command

提取命令可以轻松将应用拆分为需要添加的层Dockerfile. 这里有一个示例Dockerfile贾莫德.spring-doc.cadn.net.cn

# Perform the extraction in a separate builder container
FROM bellsoft/liberica-openjre-debian:25-cds AS builder
WORKDIR /builder
# This points to the built jar file in the target folder
# Adjust this to 'build/libs/*.jar' if you're using Gradle
ARG JAR_FILE=target/*.jar
# Copy the jar file to the working directory and rename it to application.jar
COPY ${JAR_FILE} application.jar
# Extract the jar file using an efficient layout
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted

# This is the runtime container
FROM bellsoft/liberica-openjre-debian:25-cds
WORKDIR /application
# Copy the extracted jar contents from the builder container into the working directory in the runtime container
# Every copy step creates a new docker layer
# This allows docker to only pull the changes it really needs
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
# Start the application jar - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and AOT cache (and CDS) friendly
ENTRYPOINT ["java", "-jar", "application.jar"]

假设上述情况Dockerfile在当前目录中,你的 Docker 镜像可以用Docker 构建。,或者可选地指定到你的应用jar的路径,如下例所示:spring-doc.cadn.net.cn

$ docker build --build-arg JAR_FILE=path/to/myapp.jar .

这是一个多阶段的过程Dockerfile. 构建阶段会提取后续需要的目录。 每一个复制命令与 jarmode 提取的层相关。spring-doc.cadn.net.cn

当然,一个Dockerfile可以写成而不使用贾莫德. 你可以使用以下几种组合解 压缩MV把东西移到正确的图层,但贾莫德简化了这一点。 此外,由贾莫德开箱即用,支持《进击的巨人》缓存(和CDS)。spring-doc.cadn.net.cn

AOT 缓存

如果你用的是Java < 24,AOT缓存不可用。 你必须用CDS代替。

如果你还想启用AOT缓存,可以用这个Dockerfile:spring-doc.cadn.net.cn

# Perform the extraction in a separate builder container
FROM bellsoft/liberica-openjre-debian:25-cds AS builder
WORKDIR /builder
# This points to the built jar file in the target folder
# Adjust this to 'build/libs/*.jar' if you're using Gradle
ARG JAR_FILE=target/*.jar
# Copy the jar file to the working directory and rename it to application.jar
COPY ${JAR_FILE} application.jar
# Extract the jar file using an efficient layout
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted

# This is the runtime container
FROM bellsoft/liberica-openjre-debian:25-cds
WORKDIR /application
# Copy the extracted jar contents from the builder container into the working directory in the runtime container
# Every copy step creates a new docker layer
# This allows docker to only pull the changes it really needs
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
# Execute the AOT cache training run
RUN java -XX:AOTCacheOutput=app.aot -Dspring.context.exit=onRefresh -jar application.jar
# Start the application jar with AOT cache enabled - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and AOT cache friendly
ENTRYPOINT ["java", "-XX:AOTCache=app.aot", "-jar", "application.jar"]

这和上面大致一样Dockerfile. 最后一步,它通过进行训练运行创建AOT缓存文件,并将AOT缓存参数传递给Java -jar.spring-doc.cadn.net.cn

CDS

如果你使用的是Java 24或更高版本,请使用AOT缓存而非CDS。

如果你还想启用CDS,可以用这个Dockerfile:spring-doc.cadn.net.cn

# Perform the extraction in a separate builder container
FROM bellsoft/liberica-openjre-debian:25-cds AS builder
WORKDIR /builder
# This points to the built jar file in the target folder
# Adjust this to 'build/libs/*.jar' if you're using Gradle
ARG JAR_FILE=target/*.jar
# Copy the jar file to the working directory and rename it to application.jar
COPY ${JAR_FILE} application.jar
# Extract the jar file using an efficient layout
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted

# This is the runtime container
FROM bellsoft/liberica-openjre-debian:25-cds
WORKDIR /application
# Copy the extracted jar contents from the builder container into the working directory in the runtime container
# Every copy step creates a new docker layer
# This allows docker to only pull the changes it really needs
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
# Execute the CDS training run
RUN java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar application.jar
# Start the application jar with CDS enabled - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and CDS friendly
ENTRYPOINT ["java", "-XX:SharedArchiveFile=application.jsa", "-jar", "application.jar"]

这和上面大致一样Dockerfile. 最后一步,它通过进行训练运行创建CDS归档,并将CDS参数传递给Java -jar.spring-doc.cadn.net.cn