Bake 目標

Bake 檔案中的目標(target)代表一次建構呼叫。它包含了您平常使用標誌(flags)傳遞給 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 中使用 變數,讓您的建構設定更靈活。
  • 了解如何使用矩陣在 Matrices(矩陣) 中建構具有不同設定的多個映像檔。
  • 前往 Bake 檔案參考以了解您可以在 Bake 檔案中設定的所有屬性及其語法。
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.