打包 OCI 镜像

该插件可以使用 Cloud Native Buildpacks (CNB) 从 jar 或 war 文件创建 OCI 镜像。 可以使用 build-image 目标在命令行上构建镜像。 这确保了在创建镜像之前已经运行了打包生命周期。

注意:出于安全原因,镜像以非 root 用户身份构建和运行。 有关更多详细信息,请参阅 CNB 规范

开始使用的最简单方法是在项目上调用 mvn spring-boot:build-image。 可以自动创建镜像,只要调用 package 阶段,如下例所示:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>build-image-no-fork</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

注意:将目标绑定到 package 生命周期时使用 build-image-no-fork。 此目标类似于 build-image,但不会 fork 生命周期来确保 package 已运行。 在本节的其余部分,build-image 用于指代 build-imagebuild-image-no-fork 目标。

提示:虽然 buildpack 从 可执行归档文件运行,但不需要先执行 repackage 目标,因为如果需要,会自动创建可执行归档文件。 当 build-image 重新打包应用程序时,它会应用与 repackage 目标相同的设置,也就是说,可以使用其中一个排除选项来排除依赖项。 默认情况下,spring-boot-devtoolsspring-boot-docker-compose 模块会自动排除(您可以使用 excludeDevtoolsexcludeDockerCompose 属性来控制这一点)。

Docker 守护进程

build-image 目标需要访问 Docker 守护进程。 该目标将检查本地 Docker CLI 配置文件以确定当前 上下文,并使用上下文连接信息与 Docker 守护进程通信。 如果无法确定当前上下文或上下文没有连接信息,则该目标将使用默认的本地连接。 这适用于所有支持平台上的 Docker Engine,无需配置。

可以设置环境变量来配置 build-image 目标以使用替代的本地或远程连接。 下表显示了环境变量及其值:

环境变量 描述

DOCKER_CONFIG

Docker CLI 配置文件的位置,用于确定当前上下文(默认为 $HOME/.docker

DOCKER_CONTEXT

上下文的名称,应从 Docker CLI 配置文件中检索主机信息(覆盖 DOCKER_HOST

DOCKER_HOST

包含 Docker 守护进程主机和端口的 URL - 例如 tcp://192.168.99.100:2376

DOCKER_TLS_VERIFY

设置为 1 时启用安全 HTTPS 协议(可选)

DOCKER_CERT_PATH

HTTPS 证书和密钥文件的路径(如果 DOCKER_TLS_VERIFY=1 则需要,否则忽略)

Docker 守护进程连接信息也可以使用插件配置中的 docker 参数提供。 下表总结了可用参数:

参数 描述

context

上下文的名称,应从 Docker CLI 配置文件中检索主机信息

host

包含 Docker 守护进程主机和端口的 URL - 例如 tcp://192.168.99.100:2376

tlsVerify

设置为 true 时启用安全 HTTPS 协议(可选)

certPath

HTTPS 证书和密钥文件的路径(如果 tlsVerifytrue 则需要,否则忽略)

bindHostToBuilder

当为 true 时,host 属性的值将提供给为 CNB builder 创建的容器(可选)

有关更多详细信息,另请参阅 示例

Docker 注册表

如果 builderrunImage 参数指定的 Docker 镜像存储在需要身份验证的私有 Docker 镜像注册表中,则可以使用 docker.builderRegistry 参数提供身份验证凭据。

如果生成的 Docker 镜像要发布到 Docker 镜像注册表,则可以使用 docker.publishRegistry 参数提供身份验证凭据。

为用户身份验证或身份令牌身份验证提供参数。 请查阅用于存储镜像的 Docker 注册表的文档,以获取有关支持的身份验证方法的更多信息。

下表总结了 docker.builderRegistrydocker.publishRegistry 的可用参数:

参数 描述

username

Docker 镜像注册表用户的用户名。用户身份验证需要。

password

Docker 镜像注册表用户的密码。用户身份验证需要。

url

Docker 镜像注册表的地址。用户身份验证可选。

email

Docker 镜像注册表用户的电子邮件地址。用户身份验证可选。

token

Docker 镜像注册表用户的身份令牌。令牌身份验证需要。

有关更多详细信息,另请参阅 示例

镜像自定义

该插件调用 builder 来协调镜像的生成。 builder 包含多个 buildpack,它们可以检查应用程序以影响生成的镜像。 默认情况下,该插件选择一个 builder 镜像。 生成的镜像的名称是从项目属性中推导出来的。

image 参数允许配置 builder 以及它应该如何对项目进行操作。 下表总结了可用参数及其默认值:

参数 / (用户属性) 描述 默认值

builder
(spring-boot.build-image.builder)

要使用的 builder 镜像的名称。

paketobuildpacks/builder-jammy-java-tiny:latest

trustBuilder
(spring-boot.build-image.trustBuilder)

是否将 builder 视为 受信任的

如果 builder 是 paketobuildpacks/builder-jammy-java-tinypaketobuildpacks/builder-noble-java-tinypaketobuildpacks/builder-jammy-tinypaketobuildpacks/builder-jammy-basepaketobuildpacks/builder-jammy-fullpaketobuildpacks/builder-jammy-buildpackless-tinypaketobuildpacks/builder-jammy-buildpackless-basepaketobuildpacks/builder-jammy-buildpackless-fullgcr.io/buildpacks/builderheroku/builder 之一,则为 true;否则为 false

imagePlatform
(spring-boot.build-image.imagePlatform)

要拉取的任何 builder、run 和 buildpack 镜像的平台(操作系统和架构)。 必须采用 OS[/architecture[/variant]] 的形式,例如 linux/amd64linux/arm64linux/arm/v5。 请参阅正在使用的 builder 的文档以确定可用的镜像操作系统和架构选项。

无默认值,表示应使用主机机器的平台。

runImage
(spring-boot.build-image.runImage)

要使用的 run 镜像的名称。

无默认值,表示应使用 Builder 元数据中指定的 run 镜像。

name
(spring-boot.build-image.imageName)

镜像名称 用于生成的镜像。

docker.io/library/
${project.artifactId}:${project.version}

pullPolicy
(spring-boot.build-image.pullPolicy)

策略 用于确定何时从注册表中拉取 builder 和 run 镜像。 可接受的值是 ALWAYSNEVERIF_NOT_PRESENT

ALWAYS

env

应该传递给 builder 的环境变量。

buildpacks

builder 在构建镜像时应使用的 buildpack。 将仅使用指定的 buildpack,覆盖 builder 中包含的默认 buildpack。 buildpack 引用必须采用以下形式之一:

  • builder 中的 buildpack - [urn:cnb:builder:]<buildpack ID>[@<version>]

  • 文件系统上目录中的 buildpack - [file://]<path>

  • 文件系统上 gzipped tar (.tgz) 文件中的 buildpack - [file://]<path>/<file name>

  • OCI 镜像中的 buildpack - [docker://]<host>/<repo>[:<tag>][@<digest>]

无,表示 builder 应使用其中包含的 buildpack。

bindings

卷绑定挂载 应该在构建镜像时挂载到 builder 容器。 绑定将在创建 builder 容器时未解析和未验证地传递给 Docker。 绑定必须采用以下形式之一:

  • <host source path>:<container destination path>[:<options>]

  • <host volume name>:<container destination path>[:<options>]

其中 <options> 可以包含:

  • ro 将卷作为只读挂载到容器中

  • rw 将卷作为可读写挂载到容器中

  • volume-opt=key=value 指定由选项名称及其值组成的键值对

network + (spring-boot.build-image.network)

builder 容器将配置使用的 网络驱动程序。 提供的值将在创建 builder 容器时未验证地传递给 Docker。

cleanCache + (spring-boot.build-image.cleanCache)

是否在构建前清理缓存。

false

verboseLogging

启用 builder 操作的详细日志记录。

false

publish + (spring-boot.build-image.publish)

是否将生成的镜像发布到 Docker 注册表。

false

tags

要应用于生成的镜像的一个或多个附加标签。 提供给 tags 选项的值应该是*完整*的镜像引用。 有关更多详细信息,请参阅 标签部分

buildWorkspace

builder 和 buildpack 在镜像构建期间用于存储文件的临时工作空间。 该值可以是命名卷或绑定挂载位置。

Docker 守护进程中的命名卷,名称派生自镜像名称。

buildCache

包含由 buildpack 创建并由镜像构建过程使用的层的缓存。 该值可以是命名卷或绑定挂载位置。

Docker 守护进程中的命名卷,名称派生自镜像名称。

launchCache

包含由 buildpack 创建并由镜像启动过程使用的层的缓存。 该值可以是命名卷或绑定挂载位置。

Docker 守护进程中的命名卷,名称派生自镜像名称。

createdDate
(spring-boot.build-image.createdDate)

将用于设置生成的镜像元数据中 Created 字段的日期。 该值必须是 ISO 8601 即时格式的字符串,或 now 以使用当前日期和时间。

启用 构建可重现性 的固定日期。

applicationDirectory
(spring-boot.build-image.applicationDirectory)

应用程序内容将上传到 builder 镜像中的目录的路径。 应用程序内容在生成的镜像中也将位于此位置。

/workspace

securityOptions

将应用于 builder 容器的 安全选项,作为字符串值数组提供

Linux 和 macOS 上为 ["label=disable"],Windows 上为 []

注意:该插件使用编译器的插件配置或 maven.compiler.target 属性检测项目的目标 Java 兼容性。 使用默认的 Paketo builder 和 buildpack 时,该插件指示 buildpack 安装相同的 Java 版本。 您可以按照 builder 配置 示例中所示覆盖此行为。

有关更多详细信息,另请参阅 示例

标签格式

提供给 tags 选项的值应该是*完整*的镜像引用。 接受的格式是 [domainHost:port/][path/]name[:tag][@digest]

如果缺少域,则默认为 docker.io。 如果缺少路径,则默认为 library。 如果缺少标签,则默认为 latest

一些示例:

  • my-image 导致镜像引用 docker.io/library/my-image:latest

  • my-repository/my-image 导致 docker.io/my-repository/my-image:latest

  • example.com/my-repository/my-image:1.0.0 将按原样使用

spring-boot:build-image

org.springframework.boot:spring-boot-maven-plugin:3.4.6

Package an application into an OCI image using a buildpack, forking the lifecycle to make sure that package ran. This goal is suitable for command-line invocation. If you need to configure a goal execution in your build, use build-image-no-fork instead.

Required parameters

Name Type Default

sourceDirectory

File

${project.build.directory}

Optional parameters

Name Type Default

classifier

String

docker

Docker

excludeDevtools

boolean

true

excludeDockerCompose

boolean

true

excludeGroupIds

String

excludes

List

image

Image

includeSystemScope

boolean

false

includeTools

boolean

true

includes

List

layers

Layers

layout

LayoutType

layoutFactory

LayoutFactory

loaderImplementation

LoaderImplementation

mainClass

String

skip

boolean

false

Parameter details

classifier

Classifier used when finding the source archive.

Name

classifier

Type

java.lang.String

Default value

User property

Since

2.3.0

docker

Docker configuration options.

Name

docker

Type

org.springframework.boot.maven.Docker

Default value

User property

Since

2.4.0

excludeDevtools

Exclude Spring Boot devtools from the repackaged archive.

Name

excludeDevtools

Type

boolean

Default value

true

User property

spring-boot.repackage.excludeDevtools

Since

1.3.0

excludeDockerCompose

Exclude Spring Boot dev services from the repackaged archive.

Name

excludeDockerCompose

Type

boolean

Default value

true

User property

spring-boot.repackage.excludeDockerCompose

Since

3.1.0

excludeGroupIds

Comma separated list of groupId names to exclude (exact match).

Name

excludeGroupIds

Type

java.lang.String

Default value

User property

spring-boot.excludeGroupIds

Since

1.1.0

excludes

Collection of artifact definitions to exclude. The Exclude element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier

Name

excludes

Type

java.util.List

Default value

User property

spring-boot.excludes

Since

1.1.0

image

Image configuration, with builder, runImage, name, env, cleanCache, verboseLogging, pullPolicy, and publish options.

Name

image

Type

org.springframework.boot.maven.Image

Default value

User property

Since

2.3.0

includeSystemScope

Include system scoped dependencies.

Name

includeSystemScope

Type

boolean

Default value

false

User property

Since

1.4.0

includeTools

Include JAR tools.

Name

includeTools

Type

boolean

Default value

true

User property

Since

3.3.0

includes

Collection of artifact definitions to include. The Include element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier

Name

includes

Type

java.util.List

Default value

User property

spring-boot.includes

Since

1.2.0

layers

Layer configuration with options to disable layer creation, exclude layer tools jar, and provide a custom layers configuration file.

Name

layers

Type

org.springframework.boot.maven.Layers

Default value

User property

Since

2.3.0

layout

The type of archive (which corresponds to how the dependencies are laid out inside it). Possible values are JAR, WAR, ZIP, DIR, NONE. Defaults to a guess based on the archive type.

Name

layout

Type

org.springframework.boot.maven.AbstractPackagerMojo$LayoutType

Default value

User property

Since

2.3.11

layoutFactory

The layout factory that will be used to create the executable archive if no explicit layout is set. Alternative layouts implementations can be provided by 3rd parties.

Name

layoutFactory

Type

org.springframework.boot.loader.tools.LayoutFactory

Default value

User property

Since

2.3.11

loaderImplementation

The loader implementation that should be used.

Name

loaderImplementation

Type

org.springframework.boot.loader.tools.LoaderImplementation

Default value

User property

Since

3.2.0

mainClass

The name of the main class. If not specified the first compiled class found that contains a main method will be used.

Name

mainClass

Type

java.lang.String

Default value

User property

Since

1.0.0

skip

Skip the execution.

Name

skip

Type

boolean

Default value

false

User property

spring-boot.build-image.skip

Since

2.3.0

sourceDirectory

Directory containing the source archive.

Name

sourceDirectory

Type

java.io.File

Default value

${project.build.directory}

User property

Since

2.3.0

spring-boot:build-image-no-fork

org.springframework.boot:spring-boot-maven-plugin:3.4.6

Package an application into an OCI image using a buildpack, but without forking the lifecycle. This goal should be used when configuring a goal execution in your build. To invoke the goal on the command-line, use build-image instead.

Required parameters

Name Type Default

sourceDirectory

File

${project.build.directory}

Optional parameters

Name Type Default

classifier

String

docker

Docker

excludeDevtools

boolean

true

excludeDockerCompose

boolean

true

excludeGroupIds

String

excludes

List

image

Image

includeSystemScope

boolean

false

includeTools

boolean

true

includes

List

layers

Layers

layout

LayoutType

layoutFactory

LayoutFactory

loaderImplementation

LoaderImplementation

mainClass

String

skip

boolean

false

Parameter details

classifier

Classifier used when finding the source archive.

Name

classifier

Type

java.lang.String

Default value

User property

Since

2.3.0

docker

Docker configuration options.

Name

docker

Type

org.springframework.boot.maven.Docker

Default value

User property

Since

2.4.0

excludeDevtools

Exclude Spring Boot devtools from the repackaged archive.

Name

excludeDevtools

Type

boolean

Default value

true

User property

spring-boot.repackage.excludeDevtools

Since

1.3.0

excludeDockerCompose

Exclude Spring Boot dev services from the repackaged archive.

Name

excludeDockerCompose

Type

boolean

Default value

true

User property

spring-boot.repackage.excludeDockerCompose

Since

3.1.0

excludeGroupIds

Comma separated list of groupId names to exclude (exact match).

Name

excludeGroupIds

Type

java.lang.String

Default value

User property

spring-boot.excludeGroupIds

Since

1.1.0

excludes

Collection of artifact definitions to exclude. The Exclude element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier

Name

excludes

Type

java.util.List

Default value

User property

spring-boot.excludes

Since

1.1.0

image

Image configuration, with builder, runImage, name, env, cleanCache, verboseLogging, pullPolicy, and publish options.

Name

image

Type

org.springframework.boot.maven.Image

Default value

User property

Since

2.3.0

includeSystemScope

Include system scoped dependencies.

Name

includeSystemScope

Type

boolean

Default value

false

User property

Since

1.4.0

includeTools

Include JAR tools.

Name

includeTools

Type

boolean

Default value

true

User property

Since

3.3.0

includes

Collection of artifact definitions to include. The Include element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier

Name

includes

Type

java.util.List

Default value

User property

spring-boot.includes

Since

1.2.0

layers

Layer configuration with options to disable layer creation, exclude layer tools jar, and provide a custom layers configuration file.

Name

layers

Type

org.springframework.boot.maven.Layers

Default value

User property

Since

2.3.0

layout

The type of archive (which corresponds to how the dependencies are laid out inside it). Possible values are JAR, WAR, ZIP, DIR, NONE. Defaults to a guess based on the archive type.

Name

layout

Type

org.springframework.boot.maven.AbstractPackagerMojo$LayoutType

Default value

User property

Since

2.3.11

layoutFactory

The layout factory that will be used to create the executable archive if no explicit layout is set. Alternative layouts implementations can be provided by 3rd parties.

Name

layoutFactory

Type

org.springframework.boot.loader.tools.LayoutFactory

Default value

User property

Since

2.3.11

loaderImplementation

The loader implementation that should be used.

Name

loaderImplementation

Type

org.springframework.boot.loader.tools.LoaderImplementation

Default value

User property

Since

3.2.0

mainClass

The name of the main class. If not specified the first compiled class found that contains a main method will be used.

Name

mainClass

Type

java.lang.String

Default value

User property

Since

1.0.0

skip

Skip the execution.

Name

skip

Type

boolean

Default value

false

User property

spring-boot.build-image.skip

Since

2.3.0

sourceDirectory

Directory containing the source archive.

Name

sourceDirectory

Type

java.io.File

Default value

${project.build.directory}

User property

Since

2.3.0

示例

自定义镜像构建器

如果您需要自定义用于创建镜像的构建器或用于启动构建镜像的运行镜像,请按以下示例配置插件:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<builder>mine/java-cnb-builder</builder>
						<runImage>mine/java-cnb-run</runImage>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

此配置将使用名称为 mine/java-cnb-builder 和标签 latest 的构建器镜像,以及名称为 mine/java-cnb-run 和标签 latest 的运行镜像。

也可以在命令行上指定构建器和运行镜像,如下例所示:

$ mvn spring-boot:build-image -Dspring-boot.build-image.builder=mine/java-cnb-builder -Dspring-boot.build-image.runImage=mine/java-cnb-run

构建器配置

如果构建器使用环境变量公开配置选项,则可以使用 env 属性设置这些选项。

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<BP_JVM_VERSION>17</BP_JVM_VERSION>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果 Docker 守护进程和 buildpack 下载工件的网络位置之间存在网络代理,则需要配置构建器以使用代理。 使用 Paketo 构建器时,可以通过设置 HTTPS_PROXY 和/或 HTTP_PROXY 环境变量来实现,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<HTTP_PROXY>http://proxy.example.com</HTTP_PROXY>
							<HTTPS_PROXY>https://proxy.example.com</HTTPS_PROXY>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

运行时 JVM 配置

Paketo Java buildpack 通过设置 JAVA_TOOL_OPTIONS 环境变量来配置 JVM 运行时环境。 可以修改 buildpack 提供的 JAVA_TOOL_OPTIONS 值,以自定义应用程序镜像在容器中启动时的 JVM 运行时行为。

应该存储在镜像中并应用于每个部署的环境变量修改可以按照 Paketo 文档 中所述进行设置,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<BPE_DELIM_JAVA_TOOL_OPTIONS xml:space="preserve"> </BPE_DELIM_JAVA_TOOL_OPTIONS>
							<BPE_APPEND_JAVA_TOOL_OPTIONS>-XX:+HeapDumpOnOutOfMemoryError</BPE_APPEND_JAVA_TOOL_OPTIONS>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

自定义镜像名称

默认情况下,镜像名称是从项目的 artifactIdversion 推导出来的,类似于 docker.io/library/${project.artifactId}:${project.version}。 您可以控制名称,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<name>example.com/library/${project.artifactId}</name>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

注意:此配置不提供显式标签,因此使用 latest。 也可以指定标签,使用 ${project.version}、构建中可用的任何属性或硬编码版本。

也可以在命令行上指定镜像名称,如下例所示:

$ mvn spring-boot:build-image -Dspring-boot.build-image.imageName=example.com/library/my-app:v1

Buildpack

默认情况下,构建器将使用构建器镜像中包含的 buildpack 并按预定义顺序应用它们。 可以提供替代的 buildpack 集来应用未包含在构建器中的 buildpack,或更改包含的 buildpack 的顺序。 当提供一个或多个 buildpack 时,将仅应用指定的 buildpack。

以下示例指示构建器使用打包在 .tgz 文件中的自定义 buildpack,后跟构建器中包含的 buildpack。

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildpacks>
							<buildpack>file:///path/to/example-buildpack.tgz</buildpack>
							<buildpack>urn:cnb:builder:paketo-buildpacks/java</buildpack>
						</buildpacks>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

buildpack 可以按以下任何形式指定。

位于 CNB Builder 中的 buildpack(如果构建器中只有一个匹配 buildpack-id 的 buildpack,则可以省略版本):

  • urn:cnb:builder:buildpack-id

  • urn:cnb:builder:buildpack-id@0.0.1

  • buildpack-id

  • buildpack-id@0.0.1

包含 buildpack 内容的目录路径(Windows 上不支持):

  • file:///path/to/buildpack/

  • /path/to/buildpack/

包含 buildpack 内容的 gzipped tar 文件路径:

  • file:///path/to/buildpack.tgz

  • /path/to/buildpack.tgz

包含 打包的 buildpack 的 OCI 镜像:

  • docker://example/buildpack

  • docker:///example/buildpack:latest

  • docker:///example/buildpack@sha256:45b23dee08…​

  • example/buildpack

  • example/buildpack:latest

  • example/buildpack@sha256:45b23dee08…​

镜像发布

可以通过启用 publish 选项将生成的镜像发布到 Docker 注册表。

如果 Docker 注册表需要身份验证,则可以使用 docker.publishRegistry 参数配置身份验证凭据。 如果 Docker 注册表不需要身份验证,则可以省略 docker.publishRegistry 配置。

注意:镜像将发布到的注册表由镜像名称的注册表部分(在这些示例中为 docker.example.com)确定。 如果配置了 docker.publishRegistry 凭据并包含 url 参数,则此值会传递给注册表,但不用于确定发布注册表位置。

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<name>docker.example.com/library/${project.artifactId}</name>
						<publish>true</publish>
					</image>
					<docker>
						<publishRegistry>
							<username>user</username>
							<password>secret</password>
						</publishRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

也可以在命令行上指定 publish 选项,如下例所示:

$ mvn spring-boot:build-image -Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1 -Dspring-boot.build-image.publish=true

在命令行上使用 publish 选项进行身份验证时,可以使用属性提供凭据,如下例所示:

$ mvn spring-boot:build-image \
      -Ddocker.publishRegistry.username=user \
      -Ddocker.publishRegistry.password=secret \
      -Ddocker.publishRegistry.url=docker.example.com \
      -Dspring-boot.build-image.publish=true \
      -Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1

并在 XML 配置中引用属性:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<publishRegistry>
							<url>${docker.publishRegistry.url}</url>
							<username>${docker.publishRegistry.username}</username>
							<password>${docker.publishRegistry.password}</password>
						</publishRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

构建器缓存和工作空间配置

CNB 构建器缓存用于构建和启动镜像的层。 默认情况下,这些缓存作为命名卷存储在 Docker 守护进程中,名称派生自目标镜像的完整名称。 如果镜像名称经常更改,例如当项目版本用作镜像名称中的标签时,则缓存可能会经常失效。

可以配置缓存卷以使用替代名称,以便更好地控制缓存生命周期,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildCache>
							<volume>
								<name>cache-${project.artifactId}.build</name>
							</volume>
						</buildCache>
						<launchCache>
							<volume>
								<name>cache-${project.artifactId}.launch</name>
							</volume>
						</launchCache>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

构建器和 buildpack 需要一个位置来存储镜像构建期间的临时文件。 默认情况下,此临时构建工作空间存储在命名卷中。

可以将缓存和构建工作空间配置为使用绑定挂载而不是命名卷,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildWorkspace>
							<bind>
								<source>/tmp/cache-${project.artifactId}.work</source>
							</bind>
						</buildWorkspace>
						<buildCache>
							<bind>
								<source>/tmp/cache-${project.artifactId}.build</source>
							</bind>
						</buildCache>
						<launchCache>
							<bind>
								<source>/tmp/cache-${project.artifactId}.launch</source>
							</bind>
						</launchCache>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Docker 配置

minikube 的 Docker 配置

该插件可以与 minikube 提供的 Docker 守护进程 通信,而不是默认的本地连接。

在 Linux 和 macOS 上,可以在启动 minikube 后使用命令 eval $(minikube docker-env) 设置环境变量。

也可以通过提供类似于以下示例中所示的连接详细信息来配置插件以使用 minikube 守护进程:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>tcp://192.168.99.100:2376</host>
						<tlsVerify>true</tlsVerify>
						<certPath>/home/user/.minikube/certs</certPath>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

podman 的 Docker 配置

该插件可以与 podman 容器引擎 通信。

可以通过提供类似于以下示例中所示的连接详细信息来配置插件以使用 podman 本地连接:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>unix:///run/user/1000/podman/podman.sock</host>
						<bindHostToBuilder>true</bindHostToBuilder>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

提示:安装 colima CLI 后,可以使用命令 podman info --format='{{.Host.RemoteSocket.Path}}' 获取此示例中显示的 docker.host 配置属性的值。

Colima 的 Docker 配置

该插件可以与 Colima 提供的 Docker 守护进程通信。 可以使用以下命令设置 DOCKER_HOST 环境变量:

$ export DOCKER_HOST=$(docker context inspect colima -f '{{.Endpoints.docker.Host}}')

也可以通过提供类似于以下示例中所示的连接详细信息来配置插件以使用 Colima 守护进程:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>unix:///${user.home}/.colima/docker.sock</host>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Docker 身份验证配置

如果构建器或运行镜像存储在支持用户身份验证的私有 Docker 注册表中,则可以使用 docker.builderRegistry 参数提供身份验证详细信息,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<builderRegistry>
							<username>user</username>
							<password>secret</password>
							<url>https://docker.example.com/v1/</url>
							<email>user@example.com</email>
						</builderRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果构建器或运行镜像存储在支持令牌身份验证的私有 Docker 注册表中,则可以使用 docker.builderRegistry 参数提供令牌值,如下例所示:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<builderRegistry>
							<token>9cbaf023786cd7...</token>
						</builderRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>