Dockerfile 文件

虽然只需在Dockerfile,则使用分层功能将产生优化的影像。 当您创建包含 layers 索引文件的 jar 时,spring-boot-jarmode-toolsjar 将作为依赖项添加到您的 jar 中。 在 Classpath 上使用此 jar,您可以以特殊模式启动应用程序,该模式允许引导代码运行与应用程序完全不同的东西,例如,提取层的东西。spring-doc.cadn.net.cn

toolsmode 不能与包含启动脚本的完全可执行的 Spring Boot 存档一起使用。 在构建旨在用于的 jar 文件时禁用启动脚本配置layertools.

以下是使用toolsjar 模式: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

extract命令可以轻松地将应用程序拆分为要添加到Dockerfile. 下面是一个Dockerfilejarmode.spring-doc.cadn.net.cn

# Perform the extraction in a separate builder container
FROM bellsoft/liberica-openjre-debian:17-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:17-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 CDS friendly
ENTRYPOINT ["java", "-jar", "application.jar"]

假设上述情况Dockerfile位于当前目录中,则可以使用 Docker 镜像docker build .,或者选择指定应用程序 jar 的路径,如以下示例所示:spring-doc.cadn.net.cn

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

这是一个多阶段Dockerfile. builder 阶段将提取稍后需要的目录。 每个COPY命令与 JarMode 提取的图层相关。spring-doc.cadn.net.cn

当然,一个Dockerfile可以在不使用jarmode. 您可以使用unzipmv将内容移动到正确的图层,但jarmode简化了那个。 此外,由jarmode开箱即用的 CDS 友好。spring-doc.cadn.net.cn

CDS 系列

如果您想额外启用 CDS,您可以使用它Dockerfile:spring-doc.cadn.net.cn

# Perform the extraction in a separate builder container
FROM bellsoft/liberica-openjre-debian:17-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:17-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