Bake 檔案參考
Bake 檔案是用於定義您使用 `docker buildx bake` 執行的流程的檔案。
檔案格式
您可以使用下列檔案格式定義 Bake 檔案
- HashiCorp 設定語言 (HCL)
- JSON
- YAML (Compose 檔案)
預設情況下,Bake 使用以下搜尋順序來尋找設定檔
compose.yaml
compose.yml
docker-compose.yml
docker-compose.yaml
docker-bake.json
docker-bake.override.json
docker-bake.hcl
docker-bake.override.hcl
您可以使用 `--file` 旗標明確指定檔案位置
$ docker buildx bake --file ../docker/bake.hcl --print
如果您未明確指定檔案,Bake 會在目前工作目錄中搜尋檔案。如果找到多個 Bake 檔案,所有檔案都會合併成單一定義。檔案會根據搜尋順序合併。這表示如果您的專案同時包含 `compose.yaml` 檔案和 `docker-bake.hcl` 檔案,Bake 會先載入 `compose.yaml` 檔案,然後再載入 `docker-bake.hcl` 檔案。
如果合併的檔案包含重複的屬性定義,則這些定義會根據屬性,由最後出現的定義合併或覆寫。以下屬性會被最後出現的定義覆寫
target.cache-to
target.dockerfile-inline
target.dockerfile
target.outputs
target.platforms
target.pull
target.tags
target.target
例如,如果 `compose.yaml` 和 `docker-bake.hcl` 都定義了 `tags` 屬性,則會使用 `docker-bake.hcl` 中的定義。
$ cat compose.yaml
services:
webapp:
build:
context: .
tags:
- bar
$ cat docker-bake.hcl
target "webapp" {
tags = ["foo"]
}
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": [
"webapp"
]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"tags": [
"foo"
]
}
}
}
所有其他屬性都會合併。例如,如果 `compose.yaml` 和 `docker-bake.hcl` 都定義了 `labels` 屬性的唯一項目,則會包含所有項目。相同標籤的重複項目會被覆寫。
$ cat compose.yaml
services:
webapp:
build:
context: .
labels:
com.example.foo: "foo"
com.example.name: "Alice"
$ cat docker-bake.hcl
target "webapp" {
labels = {
"com.example.bar" = "bar"
"com.example.name" = "Bob"
}
}
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": [
"webapp"
]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"labels": {
"com.example.foo": "foo",
"com.example.bar": "bar",
"com.example.name": "Bob"
}
}
}
}
語法
Bake 檔案支援以下屬性類型
- `target`:建置目標
- `group`:建置目標的集合
- `variable`:建置引數和變數
- `function`:自訂 Bake 函式
您可以在 Bake 檔案中將屬性定義為階層式區塊。您可以將一個或多個屬性指派給一個屬性。
以下程式碼片段顯示了一個簡單 Bake 檔案的 JSON 表示法。此 Bake 檔案定義了三個屬性:變數、群組和目標。
{
"variable": {
"TAG": {
"default": "latest"
}
},
"group": {
"default": {
"targets": ["webapp"]
}
},
"target": {
"webapp": {
"dockerfile": "Dockerfile",
"tags": ["docker.io/username/webapp:${TAG}"]
}
}
}
在 Bake 檔案的 JSON 表示法中,屬性是物件,而屬性是賦予這些物件的值。
以下範例顯示了 HCL 格式的相同 Bake 檔案
variable "TAG" {
default = "latest"
}
group "default" {
targets = ["webapp"]
}
target "webapp" {
dockerfile = "Dockerfile"
tags = ["docker.io/username/webapp:${TAG}"]
}
HCL 是 Bake 檔案的首選格式。除了語法差異之外,HCL 還允許您使用 JSON 和 YAML 格式不支援的功能。
本文檔中的範例使用 HCL 格式。
目標
目標反映單個 `docker build` 呼叫。請考慮以下建置指令
$ docker build \
--file=Dockerfile.webapp \
--tag=docker.io/username/webapp:latest \
https://github.com/username/webapp
您可以在 Bake 檔案中以下列方式表達此指令
target "webapp" {
dockerfile = "Dockerfile.webapp"
tags = ["docker.io/username/webapp:latest"]
context = "https://github.com/username/webapp"
}
下表顯示了您可以指派給目標的完整屬性清單
名稱 | 類型 | 說明 |
---|---|---|
args | 映射 | 建置引數 |
annotations | 清單 | 匯出器註釋 |
attest | 清單 | 建置證明 |
cache-from | 清單 | 外部快取來源 |
cache-to | 清單 | 外部快取目的地 |
context | 字串 | 位於指定路徑或 URL 中的檔案集 |
contexts | 映射 | 其他建置上下文 |
dockerfile-inline | 字串 | 內嵌 Dockerfile 字串 |
dockerfile | 字串 | Dockerfile 位置 |
inherits | 清單 | 繼承自其他目標的屬性 |
labels | 映射 | 映像檔的後設資料 |
matrix | 映射 | 定義一組將目標分支成多個目標的變數。 |
name | 字串 | 使用矩陣時覆寫目標名稱。 |
no-cache-filter | 清單 | 停用特定階段的建置快取 |
no-cache | 布林值 | 完全停用建置快取 |
output | 清單 | 輸出目的地 |
platforms | 清單 | 目標平台 |
pull | 布林值 | 永遠提取映像檔 |
secret | 清單 | 要公開給建置的機密 |
shm-size | 清單 | `/dev/shm` 的大小 |
ssh | 清單 | 要公開給建置的 SSH 代理程式通訊端或金鑰 |
tags | 清單 | 映像檔名稱與標籤 |
目標 | 字串 | 目標建置階段 |
ulimits | 清單 | Ulimit 選項 |
target.args
使用 args
屬性來定義目標的建置參數。這與將 --build-arg
旗標傳遞給建置指令的效果相同。
target "default" {
args = {
VERSION = "0.0.0+unknown"
}
}
您可以將 args
屬性設定為使用 null
值。這樣做會強制 target
使用 Dockerfile 中指定的 ARG
值。
variable "GO_VERSION" {
default = "1.20.3"
}
target "webapp" {
dockerfile = "webapp.Dockerfile"
tags = ["docker.io/username/webapp"]
}
target "db" {
args = {
GO_VERSION = null
}
dockerfile = "db.Dockerfile"
tags = ["docker.io/username/db"]
}
target.annotations
annotations
屬性可讓您將註釋新增至使用 bake 建置的映像檔。鍵值採用註釋清單,格式為 KEY=VALUE
。
target "default" {
output = ["type=image,name=foo"]
annotations = ["org.opencontainers.image.authors=dvdksn"]
}
等同於
target "default" {
output = ["type=image,name=foo,annotation.org.opencontainers.image.authors=dvdksn"]
}
預設情況下,註釋會新增到映像檔資訊清單。您可以透過在註釋前加上前綴來設定註釋的層級,前綴包含以逗號分隔的所有要註釋的層級清單。以下範例將註釋新增到映像檔索引和資訊清單。
target "default" {
output = ["type=image,name=foo"]
annotations = ["index,manifest:org.opencontainers.image.authors=dvdksn"]
}
在 指定註釋層級 中閱讀關於支援的層級。
target.attest
attest
屬性可讓您將 建置證明