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 檔案中設定的所有屬性及其語法。