Bake 中的变量

您可以在 Bake 文件中定义和使用变量来设置属性值。 将它们插值到其他值中,并执行算术运算。 变量可以使用默认值定义,并且可以用 环境变量。

使用变量作为属性值

使用variable块来定义变量。

variable "TAG" {
  default = "docker.io/username/webapp:latest"
}

以下示例演示如何使用TAG变量。

target "default" {
  context = "."
  dockerfile = "Dockerfile"
  tags = [ TAG ]
}

将变量插值

Bake 支持将变量字符串内插到值中。您可以使用该语法将变量插入到值中。以下示例 定义了一个${}TAG值为latest.

variable "TAG" {
  default = "latest"
}

要对TAG变量添加到属性的值中,请使用${TAG}语法。

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

使用--printflag 显示 解析的 build 配置。

$ docker buildx bake --print
{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": ["docker.io/username/webapp:latest"]
    }
  }
}

验证变量

要验证变量的值是否符合预期类型,请执行 value range 或其他条件,您可以使用validation块。

在以下示例中,验证用于对 变量值;这PORTvariable 必须为 1024 或更高。

# Define a variable `PORT` with a default value and a validation rule
variable "PORT" {
  default = 3000  # Default value assigned to `PORT`

  # Validation block to ensure `PORT` is a valid number within the acceptable range
  validation {
    condition = PORT >= 1024  # Ensure `PORT` is at least 1024
    error_message = "The variable 'PORT' must be 1024 or higher."  # Error message for invalid values
  }
}

如果conditionexpression 的计算结果为false,则变量值为 被视为无效,因此构建调用失败,并且error_message是 排放。例如,如果PORT=443,则条件的计算结果为false和 引发错误。

在设置验证之前,值被强制转换为预期类型。这 确保使用环境变量设置的任何覆盖都能按预期工作。

验证多个条件

要评估多个条件,请定义多个validationblocks 的 变量。所有条件都必须为true.

下面是一个示例:

# Define a variable `VAR` with multiple validation rules
variable "VAR" {
  # First validation block: Ensure the variable is not empty
  validation {
    condition = VAR != ""
    error_message = "The variable 'VAR' must not be empty."
  }

  # Second validation block: Ensure the value contains only alphanumeric characters
  validation {
    # VAR and the regex match must be identical:
    condition = VAR == regex("[a-zA-Z0-9]+", VAR)
    error_message = "The variable 'VAR' can only contain letters and numbers."
  }
}

此示例强制执行:

  • 变量不能为空。
  • 该变量必须与特定字符集匹配。

对于无效的输入,例如VAR="hello@world",则验证将失败。

验证变量依赖关系

您可以在条件表达式中引用其他 Bake 变量,从而启用 强制变量之间依赖关系的验证。这可确保 在继续之前,请正确设置因变量。

下面是一个示例:

# Define a variable `FOO`
variable "FOO" {}

# Define a variable `BAR` with a validation rule that references `FOO`
variable "BAR" {
  # Validation block to ensure `FOO` is set if `BAR` is used
  validation {
    condition = FOO != ""  # Check if `FOO` is not an empty string
    error_message = "The variable 'BAR' requires 'FOO' to be set."
  }
}

此配置可确保BARvariable 只能在以下情况下使用FOO已分配非空值。尝试在不设置FOO将触发验证错误。

转义变量插值

如果要在解析 Bake 定义时绕过变量插值,请执行以下作: 使用双美元符号 ($${VARIABLE}).

target "default" {
  dockerfile-inline = <<EOF
  FROM alpine
  ARG TARGETARCH
  RUN echo "Building for $${TARGETARCH/amd64/x64}"
  EOF
  platforms = ["linux/amd64", "linux/arm64"]
}
$ docker buildx bake --progress=plain
...
#8 [linux/arm64 2/2] RUN echo "Building for arm64"
#8 0.036 Building for arm64
#8 DONE 0.0s

#9 [linux/amd64 2/2] RUN echo "Building for x64"
#9 0.046 Building for x64
#9 DONE 0.1s
...

跨文件的变量中使用变量

当指定多个文件时,一个文件可以使用 另一个文件。在以下示例中,vars.hclfile 定义一个BASE_IMAGE变量,默认值为docker.io/library/alpine.

vars.hcl 文件
variable "BASE_IMAGE" {
  default = "docker.io/library/alpine"
}

以下内容docker-bake.hclfile 定义一个BASE_LATEST变量 引用BASE_IMAGE变量。

docker-bake.hcl
variable "BASE_LATEST" {
  default = "${BASE_IMAGE}:latest"
}

target "default" {
  contexts = {
    base = BASE_LATEST
  }
}

当您打印解析的构建配置时,使用-f标志以指定 这vars.hcldocker-bake.hcl文件中,您会看到BASE_LATEST变量解析为docker.io/library/alpine:latest.

$ docker buildx bake -f vars.hcl -f docker-bake.hcl --print app
{
  "target": {
    "default": {
      "context": ".",
      "contexts": {
        "base": "docker.io/library/alpine:latest"
      },
      "dockerfile": "Dockerfile"
    }
  }
}

其他资源

以下是一些其他资源,它们演示如何在 Bake 中使用变量:

  • 您可以覆盖variable值。有关更多信息,请参阅覆盖配置 信息。
  • 您可以在函数中引用和使用全局变量。请参阅 HCL 功能
  • 您可以在计算表达式时使用变量值。参见表达式 评估