覆寫組態

Bake 支援從檔案載入建置定義,但有時您需要更彈性的方式來配置這些定義。例如,您可能希望在特定環境中進行建置,或針對特定目標(Target)時覆寫某個屬性。

以下屬性列表可以被覆寫

  • args
  • attest
  • cache-from
  • cache-to
  • context
  • contexts
  • dockerfile
  • entitlements
  • labels
  • network(網路)
  • no-cache
  • output
  • platform
  • pull
  • secrets
  • ssh
  • tags
  • target

若要覆寫這些屬性,您可以使用以下方法

檔案覆寫

您可以載入多個定義了目標建置配置的 Bake 檔案。這在您希望將配置拆分到不同檔案以進行更好管理,或是根據載入的檔案有條件地覆寫配置時非常有用。

預設檔案查找

您可以使用 --file-f 旗標來指定要載入的檔案。如果您沒有指定任何檔案,Bake 將使用以下查找順序

  1. compose.yaml
  2. compose.yml
  3. docker-compose.yml
  4. docker-compose.yaml
  5. docker-bake.json
  6. docker-bake.hcl
  7. docker-bake.override.json
  8. docker-bake.override.hcl

如果找到多個 Bake 檔案,所有檔案都會被載入並合併為單一定義。檔案會依照查找順序進行合併。

$ docker buildx bake --print
[+] Building 0.0s (1/1) FINISHED                                                                                                                                                                                            
 => [internal] load local bake definitions                                                                                                                                                                             0.0s
 => => reading compose.yaml 45B / 45B                                                                                                                                                                                  0.0s
 => => reading docker-bake.hcl 113B / 113B                                                                                                                                                                             0.0s
 => => reading docker-bake.override.hcl 65B / 65B

如果合併後的檔案包含重複的屬性定義,這些定義會根據屬性類型的不同,進行合併或由最後出現的定義覆寫。

Bake 會嘗試依照找到的順序載入所有檔案。如果多個檔案定義了相同的目標,屬性將會進行合併或覆寫。在覆寫的情況下,最後載入的檔案具有優先權。

例如,假設有以下檔案

docker-bake.hcl
variable "TAG" {
  default = "foo"
}

target "default" {
  tags = ["username/my-app:${TAG}"]
}
docker-bake.override.hcl
variable "TAG" {
  default = "bar"
}

由於 docker-bake.override.hcl 在預設查找順序中最後載入,因此 TAG 變數會被覆寫為值 bar

$ docker buildx bake --print
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": ["username/my-app:bar"]
    }
  }
}

手動檔案覆寫

您可以使用 --file 旗標來明確指定要載入的檔案,並藉此有條件地應用覆寫檔案。

例如,您可以建立一個檔案,為特定環境定義一組配置,並僅在為該環境進行建置時載入它。以下範例顯示如何載入一個將 TAG 變數設為 baroverride.hcl 檔案。隨後 TAG 變數會在 default 目標中使用。

docker-bake.hcl
variable "TAG" {
  default = "foo"
}

target "default" {
  tags = ["username/my-app:${TAG}"]
}
overrides.hcl
variable "TAG" {
  default = "bar"
}

在不使用 --file 旗標的情況下列印建置配置,顯示 TAG 變數被設為預設值 foo

$ docker buildx bake --print
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": [
        "username/my-app:foo"
      ]
    }
  }
}

使用 --file 旗標載入 overrides.hcl 檔案,會將 TAG 變數覆寫為值 bar

$ docker buildx bake -f docker-bake.hcl -f overrides.hcl --print
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": [
        "username/my-app:bar"
      ]
    }
  }
}

命令列

您也可以使用 --set 旗標從命令列覆寫目標配置

# docker-bake.hcl
target "app" {
  args = {
    mybuildarg = "foo"
  }
}
$ docker buildx bake --set app.args.mybuildarg=bar --set app.platform=linux/arm64 app --print
{
  "group": {
    "default": {
      "targets": ["app"]
    }
  },
  "target": {
    "app": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "mybuildarg": "bar"
      },
      "platforms": ["linux/arm64"]
    }
  }
}

同樣支援在 https://golang.com.tw/pkg/path/#Match 中定義的模式比對語法

$ docker buildx bake --set foo*.args.mybuildarg=value  # overrides build arg for all targets starting with "foo"
$ docker buildx bake --set *.platform=linux/arm64      # overrides platform for all targets
$ docker buildx bake --set foo*.no-cache               # bypass caching only for targets starting with "foo"

可以使用 --set 覆寫的屬性完整列表如下

  • args
  • attest
  • cache-from
  • cache-to
  • context
  • contexts
  • dockerfile
  • entitlements
  • labels
  • network(網路)
  • no-cache
  • output
  • platform
  • pull
  • secrets
  • ssh
  • tags
  • target

環境變數

您也可以使用環境變數來覆寫配置。

Bake 允許您使用環境變數來覆寫 variable 區塊的值。只有 variable 區塊可以使用環境變數進行覆寫。這意味著您需要在 Bake 檔案中定義變數,然後設定名稱相同的環境變數來進行覆寫。

以下範例顯示如何在 Bake 檔案中定義一個帶有預設值的 TAG 變數,並使用環境變數進行覆寫。

variable "TAG" {
  default = "latest"
}

target "default" {
  context = "."
  dockerfile = "Dockerfile"
  tags = ["docker.io/username/webapp:${TAG}"]
}
$ export TAG=$(git rev-parse --short HEAD)
$ docker buildx bake --print webapp

TAG 變數被環境變數的值所覆寫,該值為 git rev-parse --short HEAD 所產生的簡短提交雜湊碼。

{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": ["docker.io/username/webapp:985e9e9"]
    }
  }
}

型別強制轉換

支援使用環境變數覆寫非字串類型的變數。作為環境變數傳遞的值會先被強制轉換為適當的型別。

以下範例定義了一個 PORT 變數。backend 目標直接使用 PORT 變數,而 frontend 目標則使用 PORT 加 1 後的值。

variable "PORT" {
  default = 3000
}

group "default" {
  targets = ["backend", "frontend"]
}

target "backend" {
  args = {
    PORT = PORT
  }
}

target "frontend" {
  args = {
    PORT = add(PORT, 1)
  }
}

使用環境變數覆寫 PORT 時,會在 frontend 目標中的運算式執行前,先將該值強制轉換為預期的整數型別。

$ PORT=7070 docker buildx bake --print
{
  "group": {
    "default": {
      "targets": [
        "backend",
        "frontend"
      ]
    }
  },
  "target": {
    "backend": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "PORT": "7070"
      }
    },
    "frontend": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "PORT": "7071"
      }
    }
  }
}
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.