远程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定义
你也可以使用cwd://前缀与-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"
]
}
}
}当你在GitHub Actions中使用远程Bake定义进行构建,并希望使用metadata-action来生成标签、注释或标签时,结合本地和远程Bake定义就变得必要了。metadata action生成一个Bake文件,该文件在运行器的本地Bake执行上下文中可用。要同时使用远程定义和本地的“仅元数据”Bake文件,请指定这两个文件,并为元数据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的身份验证策略: