Bake 文件参考

Bake 文件是一个用于定义工作流的文件,您可以使用 docker buildx bake 来运行这些工作流。

文件格式

您可以在以下文件格式中定义您的Bake文件:

  • HashiCorp 配置语言 (HCL)
  • JSON
  • YAML(Compose 文件)

默认情况下,Bake 使用以下查找顺序来查找配置文件:

  1. compose.yaml
  2. compose.yml
  3. docker-compose.yml
  4. docker-compose.yaml
  5. docker-bake.json
  6. docker-bake.override.json
  7. docker-bake.hcl
  8. 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.yamldocker-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.yamldocker-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文件的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"
}

下表显示了可以分配给目标的完整属性列表:

NameTypeDescription
argsMapBuild arguments
annotationsListExporter annotations
attestListBuild attestations
cache-fromListExternal cache sources
cache-toListExternal cache destinations
contextStringSet of files located in the specified path or URL
contextsMapAdditional build contexts
dockerfile-inlineStringInline Dockerfile string
dockerfileStringDockerfile location
inheritsListInherit attributes from other targets
labelsMapMetadata for images
matrixMapDefine a set of variables that forks a target into multiple targets.
nameStringOverride the target name when using a matrix.
no-cache-filterListDisable build cache for specific stages
no-cacheBooleanDisable build cache completely
outputListOutput destinations
platformsListTarget platforms
pullBooleanAlways pull images
secretListSecrets to expose to the build
shm-sizeListSize of /dev/shm
sshListSSH agent sockets or keys to expose to the build
tagsListImage names and tags
targetStringTarget build stage
ulimitsListUlimit options

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 属性允许您将 构建证明 应用到目标。 此属性接受证明参数的长格式CSV版本。

target "default" {
  attest = [
    "type=provenance,mode=min",
    "type=sbom"
  ]
}

target.cache-from

构建缓存源。 构建器从您指定的位置导入缓存。 它使用 Buildx 缓存存储后端, 并且它的工作方式与 --cache-from 标志相同。 这需要一个列表值,因此您可以指定多个缓存源。

target "app" {
  cache-from = [
    "type=s3,region=eu-west-1,bucket=mybucket",
    "user/repo:cache",
  ]
}

target.cache-to

构建缓存导出目的地。 构建器将其构建缓存导出到您指定的位置。 它使用 Buildx 缓存存储后端, 并且它的工作方式与 --cache-to 标志. 相同。 这需要一个列表值,因此您可以指定多个缓存导出目标。

target "app" {
  cache-to = [
    "type=s3,region=eu-west-1,bucket=mybucket",
    "type=inline"
  ]
}

target.call

指定要使用的前端方法。前端方法允许您,例如,仅执行构建检查,而不是运行构建。这与--call标志相同。

target "app" {
  call = "check"
}

有关前端方法的更多信息,请参阅CLI参考中的 docker buildx build --call

target.context

指定用于此目标的构建上下文的位置。 接受URL或目录路径。 这与您传递给构建命令的 构建上下文 位置参数相同。

target "app" {
  context = "./src/www"
}

这默认解析为当前工作目录(".")。

$ docker buildx bake --print -f - <<< 'target "default" {}'
[+] Building 0.0s (0/0)
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile"
    }
  }
}

target.contexts

额外的构建上下文。 这与 --build-context 标志相同。 此属性接受一个映射,其中的键会生成可以在构建中引用的命名上下文。

您可以指定不同类型的上下文,例如本地目录、Git URL,甚至是其他Bake目标。Bake会根据上下文值的模式自动确定上下文的类型。

Context typeExample
Container imagedocker-image://alpine@sha256:0123456789
Git URLhttps://github.com/user/proj.git
HTTP URLhttps://example.com/files
Local directory../path/to/src
Bake targettarget:base

固定镜像版本

# docker-bake.hcl
target "app" {
    contexts = {
        alpine = "docker-image://alpine:3.13"
    }
}
# Dockerfile
FROM alpine
RUN echo "Hello world"

使用本地目录

# docker-bake.hcl
target "app" {
    contexts = {
        src = "../path/to/source"
    }
}
# Dockerfile
FROM scratch AS src
FROM golang
COPY --from=src . .

使用另一个目标作为基础

注意

您应该优先使用常规的多阶段构建而不是此选项。当您有多个无法轻松合并为一个的Dockerfile时,可以使用此功能。

# docker-bake.hcl
target "base" {
    dockerfile = "baseapp.Dockerfile"
}
target "app" {
    contexts = {
        baseapp = "target:base"
    }
}
# Dockerfile
FROM baseapp
RUN echo "Hello world"

target.dockerfile-inline

使用字符串值作为构建目标的内联Dockerfile。

target "default" {
  dockerfile-inline = "FROM alpine\nENTRYPOINT [\"echo\", \"hello\"]"
}

dockerfile-inline 优先于 dockerfile 属性。 如果同时指定两者,Bake 将使用内联版本。

target.dockerfile

用于构建的Dockerfile的名称。 这与 --file 标志 对于 docker build 命令相同。

target "default" {
  dockerfile = "./src/www/Dockerfile"
}

默认解析为 "Dockerfile"

$ docker buildx bake --print -f - <<< 'target "default" {}'
[+] Building 0.0s (0/0)
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile"
    }
  }
}

target.entitlements

授权是构建过程运行所需的权限。

目前支持的权益有:

  • network.host: 允许构建使用访问主机网络的命令。在Dockerfile中,使用 RUN --network=host 来运行启用主机网络的命令。

  • security.insecure: 允许构建在不受默认安全沙箱限制的特权容器中运行命令。此类容器可能访问和修改系统资源。在Dockerfile中,使用 RUN --security=insecure 在特权容器中运行命令。

target "integration-tests" {
  # this target requires privileged containers to run nested containers
  entitlements = ["security.insecure"]
}

权限通过两步过程启用。首先,目标必须声明其所需的权限。其次,在调用bake命令时,用户必须通过传递--allow标志或在交互式终端中提示时确认权限来授予权限。这是为了确保用户意识到他们可能授予构建过程的不安全权限。

target.inherits

一个目标可以从其他目标继承属性。 使用inherits从一个目标引用到另一个目标。

在以下示例中, app-dev 目标指定了镜像名称和标签。 app-release 目标使用 inherits 来重用标签名称。

variable "TAG" {
  default = "latest"
}

target "app-dev" {
  tags = ["docker.io/username/myapp:${TAG}"]
}

target "app-release" {
  inherits = ["app-dev"]
  platforms = ["linux/amd64", "linux/arm64"]
}

inherits 属性是一个列表,意味着你可以从多个其他目标中重用属性。在以下示例中,app-release 目标重用了来自 app-dev_release 目标的属性。

target "app-dev" {
  args = {
    GO_VERSION = "1.20"
    BUILDX_EXPERIMENTAL = 1
  }
  tags = ["docker.io/username/myapp"]
  dockerfile = "app.Dockerfile"
  labels = {
    "org.opencontainers.image.source" = "https://github.com/username/myapp"
  }
}

target "_release" {
  args = {
    BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
    BUILDX_EXPERIMENTAL = 0
  }
}

target "app-release" {
  inherits = ["app-dev", "_release"]
  platforms = ["linux/amd64", "linux/arm64"]
}

当从多个目标继承属性并且存在冲突时,出现在inherits列表中的最后一个目标具有优先权。前面的例子为app-release目标定义了两次BUILDX_EXPERIMENTAL参数。它解析为0,因为_release目标出现在继承链的最后:

$ docker buildx bake --print app-release
[+] Building 0.0s (0/0)
{
  "group": {
    "default": {
      "targets": [
        "app-release"
      ]
    }
  },
  "target": {
    "app-release": {
      "context": ".",
      "dockerfile": "app.Dockerfile",
      "args": {
        "BUILDKIT_CONTEXT_KEEP_GIT_DIR": "1",
        "BUILDX_EXPERIMENTAL": "0",
        "GO_VERSION": "1.20"
      },
      "labels": {
        "org.opencontainers.image.source": "https://github.com/username/myapp"
      },
      "tags": [
        "docker.io/username/myapp"
      ],
      "platforms": [
        "linux/amd64",
        "linux/arm64"
      ]
    }
  }
}

target.labels

为构建分配图像标签。 这与docker build--label标志相同。

target "default" {
  labels = {
    "org.opencontainers.image.source" = "https://github.com/username/myapp"
    "com.docker.image.source.entrypoint" = "Dockerfile"
  }
}

可以使用null值作为标签。 如果这样做,构建器将使用Dockerfile中指定的标签值。

target.matrix

矩阵策略允许您根据指定的参数将一个目标分叉为多个不同的变体。 这与[GitHub Actions的矩阵策略]的工作方式类似。 您可以使用此策略来减少烘焙定义中的重复。

matrix 属性是一个参数名称到值列表的映射。 Bake 将每个可能的值组合构建为一个单独的目标。

每个生成的目标必须有一个唯一的名称。 要指定目标名称应如何解析,请使用name属性。

以下示例将app目标解析为app-fooapp-bar。 它还使用矩阵值来定义 目标构建阶段

target "app" {
  name = "app-${tgt}"
  matrix = {
    tgt = ["foo", "bar"]
  }
  target = tgt
}
$ docker buildx bake --print app
[+] Building 0.0s (0/0)
{
  "group": {
    "app": {
      "targets": [
        "app-foo",
        "app-bar"
      ]
    },
    "default": {
      "targets": [
        "app"
      ]
    }
  },
  "target": {
    "app-bar": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "target": "bar"
    },
    "app-foo": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "target": "foo"
    }
  }
}

多轴

您可以在矩阵中指定多个键,以在多个轴上分叉目标。 当使用多个矩阵键时,Bake 会构建所有可能的变体。

以下示例构建了四个目标:

  • app-foo-1-0
  • app-foo-2-0
  • app-bar-1-0
  • app-bar-2-0
target "app" {
  name = "app-${tgt}-${replace(version, ".", "-")}"
  matrix = {
    tgt = ["foo", "bar"]
    version = ["1.0", "2.0"]
  }
  target = tgt
  args = {
    VERSION = version
  }
}

每个矩阵目标的多个值

如果你想在矩阵中区分多个值,你可以使用映射作为矩阵值。Bake 为每个映射创建一个目标,你可以使用点符号访问嵌套的值。

以下示例构建了两个目标:

  • app-foo-1-0
  • app-bar-2-0
target "app" {
  name = "app-${item.tgt}-${replace(item.version, ".", "-")}"
  matrix = {
    item = [
      {
        tgt = "foo"
        version = "1.0"
      },
      {
        tgt = "bar"
        version = "2.0"
      }
    ]
  }
  target = item.tgt
  args = {
    VERSION = item.version
  }
}

target.name

为使用矩阵策略的目标指定名称解析。 以下示例将app目标解析为app-fooapp-bar

target "app" {
  name = "app-${tgt}"
  matrix = {
    tgt = ["foo", "bar"]
  }
  target = tgt
}

target.network

指定整个构建请求的网络模式。这将覆盖Dockerfile中所有RUN指令的默认网络模式。可接受的值是defaulthostnone

通常,为构建步骤设置网络模式的更好方法是在您的Dockerfile中使用RUN --network=。这样,您可以为各个构建步骤设置网络模式,并且每个构建Dockerfile的人都能获得一致的行为,而无需向构建命令传递额外的标志。

如果您在Bake文件中将网络模式设置为host,则在调用bake命令时还必须授予network.host权限。这是因为host网络模式需要提升权限,并且可能存在安全风险。您可以将--allow=network.host传递给docker buildx bake命令以授予权限,或者如果您使用的是交互式终端,可以在提示时确认权限。

target "app" {
  # make sure this build does not access internet
  network = "none"
}

target.no-cache-filter

不要为指定的阶段使用构建缓存。 这与docker build--no-cache-filter标志相同。 以下示例避免了foo构建阶段的构建缓存。

target "default" {
  no-cache-filter = ["foo"]
}

target.no-cache

构建镜像时不要使用缓存。 这与docker build--no-cache标志相同。

target "default" {
  no-cache = 1
}

target.output

用于导出构建输出的配置。 这与 --output 标志相同。 以下示例配置目标以使用仅缓存输出,

target "default" {
  output = ["type=cacheonly"]
}

target.platforms

设置构建目标的目标平台。 这与 --platform 标志相同。 以下示例为三种架构创建了一个多平台构建。

target "default" {
  platforms = ["linux/amd64", "linux/arm64", "linux/arm/v7"]
}

target.pull

配置构建器在构建目标时是否应尝试拉取镜像。 这与docker build--pull标志相同。 以下示例强制构建器始终拉取构建目标中引用的所有镜像。

target "default" {
  pull = true
}

target.secret

定义要暴露给构建目标的秘密。 这与 --secret 标志相同。

variable "HOME" {
  default = null
}

target "default" {
  secret = [
    "type=env,id=KUBECONFIG",
    "type=file,id=aws,src=${HOME}/.aws/credentials"
  ]
}

这让你可以在你的Dockerfile中 挂载秘密

RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
    aws cloudfront create-invalidation ...
RUN --mount=type=secret,id=KUBECONFIG,env=KUBECONFIG \
    helm upgrade --install

target.shm-size

设置在使用RUN指令时为构建容器分配的共享内存的大小。

格式为number必须大于0。单位是可选的,可以是b(字节)、k(千字节)、m(兆字节)或g(千兆字节)。如果省略单位,系统将使用字节。

这与docker build--shm-size标志相同。

target "default" {
  shm-size = "128m"
}

注意

在大多数情况下,建议让构建器自动确定适当的配置。只有在需要对复杂构建场景进行特定性能调优时,才应考虑手动调整。

target.ssh

定义要暴露给构建的SSH代理套接字或密钥。 这与 --ssh 标志相同。 如果你需要在构建期间访问私有仓库,这可能很有用。

target "default" {
  ssh = ["default"]
}
FROM alpine
RUN --mount=type=ssh \
    apk add git openssh-client \
    && install -m 0700 -d ~/.ssh \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts \
    && git clone git@github.com:user/my-private-repo.git

target.tags

用于构建目标的镜像名称和标签。 这与 --tag 标志相同。

target "default" {
  tags = [
    "org/repo:latest",
    "myregistry.azurecr.io/team/image:v1"
  ]
}

target.target

设置目标构建阶段以进行构建。 这与 --target 标志相同。

target "default" {
  target = "binaries"
}

target.ulimits

在使用RUN指令时,Ulimits会覆盖构建容器的默认ulimits,并且通过软限制和硬限制来指定,如下所示: =[:],例如:

target "app" {
  ulimits = [
    "nofile=1024:1024"
  ]
}

注意

如果你不提供一个hard limitsoft limit将被用于这两个值。如果没有设置ulimits,它们将从守护进程设置的默认ulimits继承。

注意

在大多数情况下,建议让构建器自动确定适当的配置。只有在需要对复杂构建场景进行特定性能调优时,才应考虑手动调整。

Group

组允许您一次调用多个构建(目标)。

group "default" {
  targets = ["db", "webapp-dev"]
}

target "webapp-dev" {
  dockerfile = "Dockerfile.webapp"
  tags = ["docker.io/username/webapp:latest"]
}

target "db" {
  dockerfile = "Dockerfile.db"
  tags = ["docker.io/username/db"]
}

如果组和目标同名,组优先于目标。 以下bake文件构建default组。 Bake忽略default目标。

target "default" {
  dockerfile-inline = "FROM ubuntu"
}

group "default" {
  targets = ["alpine", "debian"]
}
target "alpine" {
  dockerfile-inline = "FROM alpine"
}
target "debian" {
  dockerfile-inline = "FROM debian"
}

变量

HCL文件格式支持变量块定义。 您可以在Dockerfile中使用变量作为构建参数, 或者在Bake文件的属性值中插入它们。

variable "TAG" {
  default = "latest"
}

target "webapp-dev" {
  dockerfile = "Dockerfile.webapp"
  tags = ["docker.io/username/webapp:${TAG}"]
}

你可以在Bake文件中为变量分配一个默认值,或者为其分配一个null值。如果你分配了一个null值,Buildx将使用Dockerfile中的默认值。

您可以使用环境变量覆盖在Bake文件中设置的变量默认值。 以下示例将TAG变量设置为dev, 覆盖了前一个示例中显示的默认latest值。

$ TAG=dev docker buildx bake webapp-dev

内置变量

以下变量是内置的,您可以在使用Bake时直接使用它们,而无需定义它们。

VariableDescription
BAKE_CMD_CONTEXTHolds the main context when building using a remote Bake file.
BAKE_LOCAL_PLATFORMReturns the current platform’s default platform specification (e.g. linux/amd64).

使用环境变量作为默认值

您可以设置一个Bake变量以使用环境变量的值作为默认值:

variable "HOME" {
  default = "$HOME"
}

将变量插入属性中

要将变量插入到属性字符串值中,必须使用花括号。以下方法不起作用:

variable "HOME" {
  default = "$HOME"
}

target "default" {
  ssh = ["default=$HOME/.ssh/id_rsa"]
}

在你想插入变量的地方用大括号包裹变量:

  variable "HOME" {
    default = "$HOME"
  }

  target "default" {
-   ssh = ["default=$HOME/.ssh/id_rsa"]
+   ssh = ["default=${HOME}/.ssh/id_rsa"]
  }

在将变量插入属性之前,首先必须在bake文件中声明它,如下例所示。

$ cat docker-bake.hcl
target "default" {
  dockerfile-inline = "FROM ${BASE_IMAGE}"
}
$ docker buildx bake
[+] Building 0.0s (0/0)
docker-bake.hcl:2
--------------------
   1 |     target "default" {
   2 | >>>   dockerfile-inline = "FROM ${BASE_IMAGE}"
   3 |     }
   4 |
--------------------
ERROR: docker-bake.hcl:2,31-41: Unknown variable; There is no variable named "BASE_IMAGE"., and 1 other diagnostic(s)
$ cat >> docker-bake.hcl

variable "BASE_IMAGE" {
  default = "alpine"
}

$ docker buildx bake
[+] Building 0.6s (5/5) FINISHED

函数

一组由 通用函数 提供的 go-cty 可以在HCL文件中使用:

# docker-bake.hcl
target "webapp-dev" {
  dockerfile = "Dockerfile.webapp"
  tags = ["docker.io/username/webapp:latest"]
  args = {
    buildno = "${add(123, 1)}"
  }
}

此外, 用户定义的函数 也被支持:

# docker-bake.hcl
function "increment" {
  params = [number]
  result = number + 1
}

target "webapp-dev" {
  dockerfile = "Dockerfile.webapp"
  tags = ["docker.io/username/webapp:latest"]
  args = {
    buildno = "${increment(123)}"
  }
}

注意

查看 用户定义的HCL函数 页面以获取更多详细信息。