Bake 目标

Bake 文件中的目标代表一次构建调用。它包含您通常会使用标志传递给docker build命令的所有信息。

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

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

$ docker buildx bake webapp

您可以通过将多个目标名称传递给bake命令来同时构建多个目标。

$ docker buildx bake webapp api tests

默认目标

如果您在运行docker buildx bake时未指定目标,Bake 将构建名为default的目标。

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

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

$ docker buildx bake

目标属性

您可以为目标设置的属性与docker build的 CLI 标志非常相似,还有一些特定于 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 文件参考以了解您可以在 Bake 文件中设置的所有属性及其语法。