Dockerfile 文件
虽然只需在Dockerfile
,则使用分层功能将产生优化的影像。
当您创建包含 layers 索引文件的 jar 时,spring-boot-jarmode-tools
jar 将作为依赖项添加到您的 jar 中。
在 Classpath 上使用此 jar,您可以以特殊模式启动应用程序,该模式允许引导代码运行与应用程序完全不同的东西,例如,提取层的东西。
这tools mode 不能与包含启动脚本的完全可执行的 Spring Boot 存档一起使用。
在构建旨在用于的 jar 文件时禁用启动脚本配置layertools . |
以下是使用tools
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: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 的路径,如以下示例所示:
$ docker build --build-arg JAR_FILE=path/to/myapp.jar .
这是一个多阶段Dockerfile
.
builder 阶段将提取稍后需要的目录。
每个COPY
命令与 JarMode 提取的图层相关。
当然,一个Dockerfile
可以在不使用jarmode
.
您可以使用unzip
和mv
将内容移动到正确的图层,但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
.