Dockerfiles

虽然仅通过 Dockerfile 中的数行内容就可以将 Spring Boot uber jar 转换为 docker 映像,但我们将使用 layering feature 创建优化的 docker 映像。当你创建包含层索引文件的 jar 时,spring-boot-jarmode-layertools jar 将作为依赖项添加到你的 jar 中。通过使用 classpath 中的此 jar,你可以在特殊模式下启动你的应用程序,该模式允许引导代码运行与你的应用程序完全不同的内容,例如,提取层的内容。

While it is possible to convert a Spring Boot uber jar into a docker image with just a few lines in the Dockerfile, we will use the layering feature to create an optimized docker image. When you create a jar containing the layers index file, the spring-boot-jarmode-layertools jar will be added as a dependency to your jar. With this jar on the classpath, you can launch your application in a special mode which allows the bootstrap code to run something entirely different from your application, for example, something that extracts the layers.

layertools`模式无法与包含启动脚本的 fully executable Spring Boot archive 一起使用。在构建旨在与 `layertools 一起使用的 jar 文件时,禁用启动脚本配置。

The layertools mode can not be used with a fully executable Spring Boot archive that includes a launch script. Disable launch script configuration when building a jar file that is intended to be used with layertools.

以下是使用 `layertools`jar 模式的启动 jar 的方法:

Here’s how you can launch your jar with a layertools jar mode:

$ java -Djarmode=layertools -jar my-app.jar

这将提供以下输出:

This will provide the following output:

Usage:
  java -Djarmode=layertools -jar my-app.jar

Available commands:
  list     List layers from the jar that can be extracted
  extract  Extracts layers from the jar for image creation
  help     Help about any command

extract 命令可用于轻松将应用程序拆分成要添加到 Dockerfile 的层。以下是一个使用 jarmode 的 Dockerfile 示例。

The extract command can be used to easily split the application into layers to be added to the dockerfile. Here is an example of a Dockerfile using jarmode.

FROM eclipse-temurin:17-jre as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM eclipse-temurin:17-jre
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]

假定上述 Dockerfile 在当前目录中,则可以使用 docker build . 构建你的 docker 映像,或选择指定你的应用程序 jar 的路径,如下例中所示:

Assuming the above Dockerfile is in the current directory, your docker image can be built with docker build ., or optionally specifying the path to your application jar, as shown in the following example:

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

这是一个多阶段 dockerfile。builder 阶段会提取之后需要使用的目录。每个 COPY 命令都与通过 jarmode 提取的层相关。

This is a multi-stage dockerfile. The builder stage extracts the directories that are needed later. Each of the COPY commands relates to the layers extracted by the jarmode.

当然,可以在不使用 jarmode 的情况下编写 Dockerfile。你可以使用 unzipmv 的某种组合将内容移到右侧层,但 jarmode 可简化此操作。

Of course, a Dockerfile can be written without using the jarmode. You can use some combination of unzip and mv to move things to the right layer but jarmode simplifies that.