烘焙目标

Bake 文件中的目标表示构建调用。它包含所有 您通常会传递给docker build命令。

target "webapp" {
  dockerfile = "webapp.Dockerfile"
  tags = ["docker.io/username/webapp:latest"]
  context = "https://github.com/username/webapp"
}

要使用 Bake 构建目标,请将目标的 name 传递给bake命令。

$ docker buildx bake webapp

您可以通过将多个目标名称传递给bake命令。

$ docker buildx bake webapp api tests

默认目标

如果您在运行时未指定目标docker buildx bake、Bake will 构建名为default.

target "default" {
  dockerfile = "webapp.Dockerfile"
  tags = ["docker.io/username/webapp:latest"]
  context = "https://github.com/username/webapp"
}

要构建此目标,请运行docker buildx bake不带任何参数:

$ docker buildx bake

目标属性

您可以为目标设置的属性与 CLI 标志非常相似docker build,以及一些特定于 Bake 的附加属性。

有关可为目标设置的所有属性,请参阅 Bake 参考

对目标进行分组

您可以使用group块。这在以下情况下非常有用 想要一次构建多个目标。

group "all" {
  targets = ["webapp", "api", "tests"]
}

target "webapp" {
  dockerfile = "webapp.Dockerfile"
  tags = ["docker.io/username/webapp:latest"]
  context = "https://github.com/username/webapp"
}

target "api" {
  dockerfile = "api.Dockerfile"
  tags = ["docker.io/username/api:latest"]
  context = "https://github.com/username/api"
}

target "tests" {
  dockerfile = "tests.Dockerfile"
  contexts = {
    webapp = "target:webapp",
    api = "target:api",
  }
  output = ["type=local,dest=build/tests"]
  context = "."
}

要构建组中的所有目标,请将组的名称传递给bake命令。

$ docker buildx bake all

其他资源

请参阅以下页面以了解有关 Bake 功能的更多信息:

  • 了解如何在 Bake 中使用变量进行构建 配置更灵活。
  • 了解如何使用矩阵构建具有不同 矩阵中的配置。
  • 前往 Bake file 参考 了解全部 可以在 Bake 文件中设置的属性及其语法。