Dockerfiles

虽然只需在Dockerfile中添加几行代码即可将 Spring Boot 胖 JAR 文件转换为 Docker 镜像,但使用分层功能将生成一个优化的镜像。当您创建包含层索引文件的 JAR 文件时,spring-boot-jarmode-tools JAR 文件将作为依赖项添加到您的 JAR 文件中。将此 JAR 文件添加到类路径后,您可以在特殊模式下启动应用程序,该模式允许引导代码运行与应用程序完全不同的内容,例如,提取层的内容。

tools 模式不能与包含启动脚本的完全可执行的 Spring Boot 归档文件一起使用。在构建打算与layertools一起使用的 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中。以下是用jarmodeDockerfile示例:

# 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 build .构建 Docker 镜像,或者可以选择指定应用程序 JAR 文件的路径,如下例所示:

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

这是一个多阶段Dockerfile。构建器阶段提取稍后需要的目录。每个COPY命令都与jarmode提取的层相关联。

当然,可以不用jarmode编写Dockerfile。您可以结合使用unzipmv将文件移动到正确的层,但jarmode简化了此过程。此外,jarmode创建的布局默认情况下是 CDS 友好的。

CDS

如果您还希望启用CDS,可以使用此Dockerfile

# 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