Bake 中的變數

您可以在 Bake 檔案中定義並使用變數,以設定屬性值、將其插入其他值中,並進行算術運算。變數可以定義預設值,也可以透過環境變數進行覆寫。

使用變數作為屬性值

使用 variable 區塊來定義變數。

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

以下範例展示了如何在 target 中使用 TAG 變數。

docker-bake.hcl
target "webapp" {
  context = "."
  dockerfile = "Dockerfile"
  tags = [ TAG ]
}

將變數插入值中

Bake 支援將變數進行字串插值並代入值中。您可以使用 ${} 語法將變數插入值內。以下範例定義了一個值為 latestTAG 變數。

docker-bake.hcl
variable "TAG" {
  default = "latest"
}

若要將 TAG 變數插入屬性值中,請使用 ${TAG} 語法。

docker-bake.hcl
group "default" {
  targets = [ "webapp" ]
}

variable "TAG" {
  default = "latest"
}

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

使用 --print 旗標列印 Bake 檔案時,會顯示解析後的建置設定中已插入變數的值。

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

驗證變數

若要驗證變數的值是否符合預期的類型、數值範圍或其他條件,您可以使用 validation 區塊定義自訂驗證規則。

在以下範例中,驗證用於對變數值執行數值限制;PORT 變數必須大於或等於 1024。

docker-bake.hcl
# 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 greater."  # Error message for invalid values
  }
}

如果 condition 表達式的評估結果為 false,則變數值被視為無效,建置呼叫將失敗並發出 error_message。例如,如果 PORT=443,條件評估結果為 false,則會引發錯誤。

在設定驗證之前,值會被強制轉換為預期類型。這確保了使用環境變數設定的任何覆寫都能按預期運作。

驗證多個條件

若要評估多個條件,請為該變數定義多個 validation 區塊。所有條件都必須為 true

以下是一個範例

docker-bake.hcl
# 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 變數,從而啟用強制變數間依賴關係的驗證。這確保了在進行下一步之前,依賴變數已正確設定。

以下是一個範例

docker-bake.hcl
# 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."
  }
}

此設定確保只有在 FOO 被指派為非空值時,才能使用 BAR 變數。如果未設定 FOO 而嘗試進行建置,將會觸發驗證錯誤。

跳脫變數插值

如果您想在解析 Bake 定義時略過變數插值,請使用雙美元符號 ($${VARIABLE})。

docker-bake.hcl
target "webapp" {
  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.hcl 檔案定義了一個預設值為 docker.io/library/alpineBASE_IMAGE 變數。

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

以下的 docker-bake.hcl 檔案定義了一個參照 BASE_IMAGE 變數的 BASE_LATEST 變數。

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

target "webapp" {
  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": {
    "webapp": {
      "context": ".",
      "contexts": {
        "base": "docker.io/library/alpine:latest"
      },
      "dockerfile": "Dockerfile"
    }
  }
}

其他資源

以下是一些額外的資源,展示了如何在 Bake 中使用變數

  • 您可以使用環境變數來覆寫 variable 值。請參閱 覆寫設定 以取得更多資訊。
  • 您可以在函式中參照並使用全域變數。請參閱 HCL 函式
  • 您可以在評估表達式時使用變數值。請參閱 表達式評估
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.