Bake 目标

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

docker-bake.hcl
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` 的目标。

docker-bake.hcl
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` 块将目标分组。当您想一次构建多个目标时,这非常有用。

docker-bake.hcl
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 文件中设置的所有属性及其语法。
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.