Bake 中的變數

您可以在 Bake 檔案中定義和使用變數來設定屬性值、將它們插入其他值中,並執行算術運算。變數可以定義預設值,並且可以使用環境變數覆寫。

使用變數作為屬性值

使用 variable 區塊來定義變數。

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

以下範例顯示如何在目標中使用 TAG 變數。

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

將變數插入值中

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

variable "TAG" {
  default = "latest"
}

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

target "default" {
  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。

# 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
  }
}

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

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

驗證多個條件

要評估多個條件,請為變數定義多個 validation 區塊。所有條件都必須為 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."
  }
}

此設定可確保只有在 FOO 已指派非空值時才能使用 BAR 變數。嘗試在未設定 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.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 "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 函數
  • 您可以在評估運算式時使用變數值。請參閱運算式評估