docker buildx bake
描述 | 從檔案構建 |
---|---|
用法 | docker buildx bake [選項] [目標...] |
別名 | docker buildx f |
描述
Bake 是一個高階構建命令。每個指定的目標都將作為構建的一部分並行執行。
請閱讀 使用 Bake 的高階構建選項 指南,瞭解如何編寫 bake 檔案。
注意未來,如果需要,
buildx bake
命令可能會收到向後不相容的功能。我們正在尋求關於改進命令和進一步擴充套件功能的反饋。
選項
選項 | 預設值 | 描述 |
---|---|---|
--allow | 允許構建訪問指定的資源 | |
--call | build | 設定評估構建的方法(check ,outline ,targets ) |
--check | --call=check 的簡寫 | |
-f, --file | 構建定義檔案 | |
--list | 列出目標或變數 | |
--load | --set=*.output=type=docker 的簡寫 | |
--metadata-file | 將構建結果元資料寫入檔案 | |
--no-cache | 構建映象時不使用快取 | |
--print | 列印選項而不構建 | |
--progress | auto | 設定進度輸出型別(auto ,quiet ,plain ,tty ,rawjson )。使用 plain 顯示容器輸出 |
--provenance | --set=*.attest=type=provenance 的簡寫 | |
--pull | 始終嘗試拉取所有引用的映象 | |
--push | --set=*.output=type=registry 的簡寫 | |
--sbom | --set=*.attest=type=sbom 的簡寫 | |
--set | 覆蓋目標值(例如,targetpattern.key=value ) |
示例
允許額外的特權授權 (--allow)
--allow=ENTITLEMENT[=VALUE]
授權旨在提供對特權操作的受控訪問。預設情況下,Buildx 和 BuildKit 在受限許可權下執行,以保護使用者及其系統免受意外的副作用或安全風險。--allow
標誌明確授予對額外授權的訪問許可權,明確表示構建或烘焙操作何時需要提升的許可權。
除了 BuildKit 的 network.host
和 security.insecure
授權(請參閱 docker buildx build --allow
),Bake 還支援檔案系統授權,可以對檔案系統訪問進行精細控制。當處理需要訪問預設工作目錄之外的檔案的構建時,這些特別有用。
Bake 支援以下檔案系統授權
--allow fs=<path|*>
- 授予對工作目錄之外的檔案的讀寫訪問許可權。--allow fs.read=<path|*>
- 授予對工作目錄之外的檔案的只讀訪問許可權。--allow fs.write=<path|*>
- 授予對工作目錄之外的檔案的寫訪問許可權。
fs
授權接受檔案系統上目錄的路徑值(相對或絕對)。或者,您可以傳遞萬用字元(*
)以允許 Bake 訪問整個檔案系統。
示例:fs.read
給定以下 Bake 配置,Bake 需要訪問相對於 Bake 檔案的父目錄。
target "app" {
context = "../src"
}
假設 docker buildx bake app
與 docker-bake.hcl
檔案在同一目錄中執行,您需要明確允許 Bake 從 ../src
目錄讀取。在這種情況下,以下所有呼叫都有效
$ docker buildx bake --allow fs.read=* app
$ docker buildx bake --allow fs.read=../src app
$ docker buildx bake --allow fs=* app
示例:fs.write
以下 docker-bake.hcl
檔案需要對 /tmp
目錄的寫訪問許可權。
target "app" {
output = "/tmp"
}
假設 docker buildx bake app
在 /tmp
目錄之外執行,您需要允許 fs.write
授權,可以透過指定路徑或使用萬用字元
$ docker buildx bake --allow fs=/tmp app
$ docker buildx bake --allow fs.write=/tmp app
$ docker buildx bake --allow fs.write=* app
覆蓋已配置的構建器例項(--builder)
呼叫前端方法 (--call)
與 build --call
相同。
呼叫:檢查 (--check)
與 build --check
相同。
指定構建定義檔案 (-f, --file)
使用 -f
/ --file
選項指定要使用的構建定義檔案。該檔案可以是 HCL、JSON 或 Compose 檔案。如果指定了多個檔案,則所有檔案都將被讀取,並且構建配置將合併。
或者,可以使用環境變數 BUILDX_BAKE_FILE
來指定要使用的構建定義。這與 -f
/ --file
是互斥的;如果兩者都指定,則忽略環境變數。可以透過使用系統的路徑分隔符(Windows 上通常為 ;
,其他地方為 :
)來分隔多個定義,但可以使用 BUILDX_BAKE_PATH_SEPARATOR
進行更改。
您可以傳遞要構建的目標名稱,以僅構建特定目標。以下示例構建 docker-bake.dev.hcl
檔案中定義的 db
和 webapp-release
目標
# docker-bake.dev.hcl
group "default" {
targets = ["db", "webapp-dev"]
}
target "webapp-dev" {
dockerfile = "Dockerfile.webapp"
tags = ["docker.io/username/webapp"]
}
target "webapp-release" {
inherits = ["webapp-dev"]
platforms = ["linux/amd64", "linux/arm64"]
}
target "db" {
dockerfile = "Dockerfile.db"
tags = ["docker.io/username/db"]
}
$ docker buildx bake -f docker-bake.dev.hcl db webapp-release
有關更多詳細資訊,請參閱 Bake 檔案參考。
列出目標和變數 (--list)
--list
標誌顯示 Bake 配置中所有可用的目標或變數,以及描述(如果在 Bake 檔案中使用 description
屬性設定)。
列出所有目標
$ docker buildx bake --list=targets
TARGET DESCRIPTION
binaries
default binaries
update-docs
validate
validate-golangci Validate .golangci.yml schema (does not run Go linter)
列出變數
$ docker buildx bake --list=variables
VARIABLE TYPE VALUE DESCRIPTION
REGISTRY string docker.io/username Registry and namespace
IMAGE_NAME string my-app Image name
GO_VERSION <null>
DEBUG bool false Add debug symbols
變數型別將在 Bake 檔案中使用 type
屬性設定時顯示。
預設情況下,docker buildx bake --list
的輸出以表格形式呈現。或者,您可以使用長格式 CSV 語法並指定 format
屬性以 JSON 格式輸出列表。
$ docker buildx bake --list=type=targets,format=json
將構建結果元資料寫入檔案 (--metadata-file)
類似於 buildx build --metadata-file
,但為每個目標寫入結果對映,例如
# docker-bake.hcl
group "default" {
targets = ["db", "webapp-dev"]
}
target "db" {
dockerfile = "Dockerfile.db"
tags = ["docker.io/username/db"]
}
target "webapp-dev" {
dockerfile = "Dockerfile.webapp"
tags = ["docker.io/username/webapp"]
}
$ docker buildx bake --load --metadata-file metadata.json .
$ cat metadata.json
{
"buildx.build.warnings": {},
"db": {
"buildx.build.provenance": {},
"buildx.build.ref": "mybuilder/mybuilder0/0fjb6ubs52xx3vygf6fgdl611",
"containerimage.config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
"containerimage.descriptor": {
"annotations": {
"config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
"org.opencontainers.image.created": "2022-02-08T21:28:03Z"
},
"digest": "sha256:19ffeab6f8bc9293ac2c3fdf94ebe28396254c993aea0b5a542cfb02e0883fa3",
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 506
},
"containerimage.digest": "sha256:19ffeab6f8bc9293ac2c3fdf94ebe28396254c993aea0b5a542cfb02e0883fa3"
},
"webapp-dev": {
"buildx.build.provenance": {},
"buildx.build.ref": "mybuilder/mybuilder0/kamngmcgyzebqxwu98b4lfv3n",
"containerimage.config.digest": "sha256:9651cc2b3c508f697c9c43b67b64c8359c2865c019e680aac1c11f4b875b67e0",
"containerimage.descriptor": {
"annotations": {
"config.digest": "sha256:9651cc2b3c508f697c9c43b67b64c8359c2865c019e680aac1c11f4b875b67e0",
"org.opencontainers.image.created": "2022-02-08T21:28:15Z"
},
"digest": "sha256:6d9ac9237a84afe1516540f40a0fafdc86859b2141954b4d643af7066d598b74",
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 506
},
"containerimage.digest": "sha256:6d9ac9237a84afe1516540f40a0fafdc86859b2141954b4d643af7066d598b74"
}
}
注意構建記錄 出處 (
buildx.build.provenance
) 預設包含最少出處。設定BUILDX_METADATA_PROVENANCE
環境變數以自定義此行為
min
設定最小出處(預設)。max
設定完整出處。disabled
、false
或0
不設定任何出處。
注意預設情況下不包含構建警告 (
buildx.build.warnings
)。將BUILDX_METADATA_WARNINGS
環境變數設定為1
或true
以包含它們。
構建映象時不要使用快取 (--no-cache)
與 build --no-cache
相同。構建映象時不使用快取。
列印選項而不構建 (--print)
以 JSON 格式列印所需構建目標的最終選項,而不啟動構建。
$ docker buildx bake -f docker-bake.hcl --print db
{
"group": {
"default": {
"targets": [
"db"
]
}
},
"target": {
"db": {
"context": "./",
"dockerfile": "Dockerfile",
"tags": [
"docker.io/tiborvass/db"
]
}
}
}
設定進度輸出型別(--progress)
與 build --progress
相同。
創建出處證明 (--provenance)
與 build --provenance
相同。
始終嘗試拉取更新版本的映象 (--pull)
與 build --pull
相同。
建立 SBOM 證明 (--sbom)
與 build --sbom
相同。
從命令列覆蓋目標配置 (--set)
--set targetpattern.key[.subkey]=value
從命令列覆蓋目標配置。模式匹配語法在 https://golang.org.tw/pkg/path/#Match 中定義。
$ docker buildx bake --set target.args.mybuildarg=value
$ docker buildx bake --set target.platform=linux/arm64
$ 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'
$ docker buildx bake --set target.platform+=linux/arm64 # appends 'linux/arm64' to the platform list
您可以覆蓋以下欄位
annotations
attest
args
cache-from
cache-to
call
context
dockerfile
entitlements
extra-hosts
labels
load
no-cache
no-cache-filter
output
platform
pull
push
secrets
ssh
tags
target
您可以使用 +=
運算子追加以下欄位
annotations
¹attest
¹cache-from
cache-to
entitlements
¹no-cache-filter
output
platform
secrets
ssh
tags
注意¹ 這些欄位預設已追加。