Dockerfiles

雖然可以使用 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 中。以下是使用 jarmodeDockerfile 示例。

# 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 build . 構建,或者選擇性地指定您的應用程式 JAR 包的路徑,如以下示例所示

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

這是一個多階段 Dockerfile。構建器階段提取以後需要的目錄。每個 COPY 命令都與 jarmode 提取的層相關。

當然,Dockerfile 也可以不使用 jarmode 來編寫。您可以使用 unzipmv 的組合將檔案移動到正確的層,但 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

© . This site is unofficial and not affiliated with VMware.