Dockerfiles
雖然可以僅在 Dockerfile
中使用幾行程式碼將 Spring Boot uber 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
。以下是使用 jarmode
的 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/ ./
# 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
。建置器階段提取稍後需要的目錄。每個 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
。