遠端 Bake 檔案定義
您可以直接從遠端 Git 倉庫或 HTTPS URL 構建 Bake 檔案
$ docker buildx bake "https://github.com/docker/cli.git#v20.10.11" --print
#1 [internal] load git source https://github.com/docker/cli.git#v20.10.11
#1 0.745 e8f1871b077b64bcb4a13334b7146492773769f7 refs/tags/v20.10.11
#1 2.022 From https://github.com/docker/cli
#1 2.022 * [new tag] v20.10.11 -> v20.10.11
#1 DONE 2.9s
這將從指定的遠端位置獲取 Bake 定義並執行該檔案中定義的分組或目標。如果遠端 Bake 定義未指定構建上下文,則上下文會自動設定為 Git 遠端。例如,此示例使用 https://github.com/docker/cli.git
{
"group": {
"default": {
"targets": ["binary"]
}
},
"target": {
"binary": {
"context": "https://github.com/docker/cli.git#v20.10.11",
"dockerfile": "Dockerfile",
"args": {
"BASE_VARIANT": "alpine",
"GO_STRIP": "",
"VERSION": ""
},
"target": "binary",
"platforms": ["local"],
"output": ["build"]
}
}
}
將本地上下文與遠端定義一起使用
在使用遠端 Bake 定義進行構建時,您可能希望使用相對於 Bake 命令執行目錄的本地檔案。您可以使用 cwd://
字首將上下文定義為相對於命令上下文。
target "default" {
context = "cwd://"
dockerfile-inline = <<EOT
FROM alpine
WORKDIR /src
COPY . .
RUN ls -l && stop
EOT
}
$ touch foo bar
$ docker buildx bake "https://github.com/dvdksn/buildx.git#bake-remote-example"
...
> [4/4] RUN ls -l && stop:
#8 0.101 total 0
#8 0.102 -rw-r--r-- 1 root root 0 Jul 27 18:47 bar
#8 0.102 -rw-r--r-- 1 root root 0 Jul 27 18:47 foo
#8 0.102 /bin/sh: stop: not found
如果您想使用特定的本地目錄作為上下文,可以將路徑附加到 cwd://
字首。請注意,如果您確實指定了路徑,則它必須位於執行命令的工作目錄內。如果您使用絕對路徑,或指向工作目錄之外的相對路徑,Bake 將丟擲錯誤。
本地命名上下文
您還可以使用 cwd://
字首將 Bake 執行上下文中的本地目錄定義為命名上下文。
以下示例將 docs
上下文定義為 ./src/docs/content
,相對於 Bake 執行的當前工作目錄作為命名上下文。
target "default" {
contexts = {
docs = "cwd://src/docs/content"
}
dockerfile = "Dockerfile"
}
相比之下,如果您省略 cwd://
字首,路徑將相對於構建上下文解析。
指定要使用的 Bake 定義
從遠端 Git 倉庫載入 Bake 檔案時,如果倉庫包含多個 Bake 檔案,您可以使用 --file
或 -f
標誌指定要使用的 Bake 定義
docker buildx bake -f bake.hcl "https://github.com/crazy-max/buildx.git#remote-with-local"
...
#4 [2/2] RUN echo "hello world"
#4 0.270 hello world
#4 DONE 0.3s
合併本地和遠端 Bake 定義
您還可以使用 -f
結合 cwd://
字首將遠端定義與本地定義合併。
給定當前工作目錄中的以下本地 Bake 定義
# local.hcl
target "default" {
args = {
HELLO = "foo"
}
}
以下示例使用 -f
指定兩個 Bake 定義
-f bake.hcl
:此定義是相對於 Git URL 載入的。-f cwd://local.hcl
:此定義是相對於 Bake 命令執行的當前工作目錄載入的。
docker buildx bake -f bake.hcl -f cwd://local.hcl "https://github.com/crazy-max/buildx.git#remote-with-local" --print
{
"target": {
"default": {
"context": "https://github.com/crazy-max/buildx.git#remote-with-local",
"dockerfile": "Dockerfile",
"args": {
"HELLO": "foo"
},
"target": "build",
"output": [
{
"type": "cacheonly"
}
]
}
}
}
合併本地和遠端 Bake 定義變得必要的一種情況是,當您在 GitHub Actions 中使用遠端 Bake 定義進行構建,並希望使用 metadata-action 生成標籤、註釋或標籤時。元資料操作會在執行器的本地 Bake 執行上下文中生成一個 Bake 檔案。要同時使用遠端定義和本地“僅元資料”Bake 檔案,請指定這兩個檔案,並對元資料 Bake 檔案使用 cwd://
字首
- name: Build
uses: docker/bake-action@v6
with:
files: |
./docker-bake.hcl
cwd://${{ steps.meta.outputs.bake-file }}
targets: build
私有倉庫中的遠端定義
如果您想使用私有倉庫中的遠端定義,您可能需要指定 Bake 在獲取定義時使用的憑據。
如果您可以使用預設的 SSH_AUTH_SOCK
對私有倉庫進行身份驗證,那麼您無需為 Bake 指定任何額外的身份驗證引數。Bake 會自動使用您的預設代理套接字。
對於使用 HTTP 令牌或自定義 SSH 代理進行身份驗證,請使用以下環境變數來配置 Bake 的身份驗證策略