远程烘焙文件定义
您可以直接从远程 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
definition 未指定构建上下文,则上下文会自动设置为
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 定义进行构建时,您可能希望使用本地
文件。您可以
使用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://
prefix (如果要使用特定的
local 目录作为上下文。请注意,如果您指定了路径,则它必须是
在执行命令的工作目录中。如果您使用
绝对路径,或指向工作目录外部的相对路径,
Bake 将引发错误。
本地命名上下文
您还可以使用cwd://
前缀来定义 Bake 中的本地目录
execution context 作为命名上下文。
以下示例定义了docs
context 设置为./src/docs/content
,
相对于 Bake 作为命名上下文运行的当前工作目录。
target "default" {
contexts = {
docs = "cwd://src/docs/content"
}
dockerfile = "Dockerfile"
}
相比之下,如果省略cwd://
prefix 时,将解析该路径
相对于 build 上下文。
指定要使用的 Bake definition
从远程 Git 仓库加载 Bake 文件时,如果仓库的
包含多个 Bake 文件,您可以指定要使用的 Bake 定义
使用--file
或-f
旗:
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 定义
您还可以使用cwd://
prefix 替换为-f
.
给定当前工作目录中的以下本地 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 来
生成标记、注释或标签。metadata作会生成一个 Bake
文件。要使用
remote 定义和本地 “metadata-only” Bake 文件,同时指定这两个文件
并使用cwd://
前缀:
- name: Build
uses: docker/bake-action@v4
with:
source: "${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}"
files: |
./docker-bake.hcl
cwd://${{ steps.meta.outputs.bake-file }}
targets: build
私有存储库中的远程定义
如果要使用位于私有存储库中的远程定义, 您可能需要指定 Bake 在获取定义时要使用的凭据。
如果您可以使用默认的SSH_AUTH_SOCK
,
则无需为 Bake 指定任何其他身份验证参数。
Bake 会自动使用默认代理套接字。
对于使用 HTTP 令牌或自定义 SSH 代理的身份验证, 使用以下环境变量配置 Bake 的身份验证策略: