|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Boot 4.0.4! |
Dockerfile 文件
虽然仅需在 Dockerfile 中添加几行即可将 Spring Boot 超级 jar 转换为 Docker 镜像,但使用 分层功能 将生成一个优化的镜像。
当您创建包含分层索引文件的 jar 时,spring-boot-jarmode-tools jar 将作为依赖项添加到您的 jar 中。
有了这个 jar 在类路径上,您可以以特殊模式启动您的应用程序,这允许引导代码执行与您的应用程序完全不同的内容,例如,提取分层内容。
以下是如何使用 tools jar 模式启动你的 jar 包:
$ java -Djarmode=tools -jar my-app.jar
这将提供以下输出:
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
extract 命令可用于轻松地将应用程序分成层,以添加到 Dockerfile 中。
这是使用 Dockerfile 的一个 jarmode 示例。
# 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 build . 构建 Docker 镜像,或者选择性地指定应用程序 jar 包的路径,如下例所示:
$ docker build --build-arg JAR_FILE=path/to/myapp.jar .
这是一个多阶段的 Dockerfile。
构建器阶段会提取后续所需的目录。
每个 COPY 命令都与由 jarmode 提取的层相关。
当然,可以不用jarmode来编写一个Dockerfile。
您可以使用unzip和mv的一些组合将内容移动到右层,但jarmode简化了这一过程。
此外,由jarmode创建的布局默认情况下就对AOT缓存(和CDS)友好。
AOT 缓存
| 如果你使用的是 Java < 24,AOT 缓存不可用。 你必须改用 CDS。 |
如果您想进一步启用 AOT 缓存,可以使用此 Dockerfile:
# 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。
CDS
| 如果你使用的是 Java 24 或更高版本,请改用 AOT 缓存而不是 CDS。 |
如果您想额外启用 CDS,可以使用此 Dockerfile:
# 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。