Variables in Bake

You can define and use variables in a Bake file to set attribute values, interpolate them into other values, and perform arithmetic operations. Variables can be defined with default values, and can be overridden with environment variables.

Using variables as attribute values

Use the variable block to define a variable.

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

The following example shows how to use the TAG variable in a target.

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

Interpolate variables into values

Bake supports string interpolation of variables into values. You can use the ${} syntax to interpolate a variable into a value. The following example defines a TAG variable with a value of latest.

variable "TAG" {
  default = "latest"
}

To interpolate the TAG variable into the value of an attribute, use the ${TAG} syntax.

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

Printing the Bake file with the --print flag shows the interpolated value in the resolved build configuration.

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

Using variables in variables across files

When multiple files are specified, one file can use variables defined in another file. In the following example, the vars.hcl file defines a BASE_IMAGE variable with a default value of docker.io/library/alpine.

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

The following docker-bake.hcl file defines a BASE_LATEST variable that references the BASE_IMAGE variable.

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

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

When you print the resolved build configuration, using the -f flag to specify the vars.hcl and docker-bake.hcl files, you see that the BASE_LATEST variable is resolved to 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"
    }
  }
}

Additional resources

Here are some additional resources that show how you can use variables in Bake: