- The variable must not be empty.
- The variable must match a specific character set.
For invalid inputs like `VAR="hello@world"`, the validation would fail.
### Validating variable dependencies
You can reference other Bake variables in your condition expression, enabling
validations that enforce dependencies between variables. This ensures that
dependent variables are set correctly before proceeding.
Here’s an example:
```hcl {title=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."
}
}
```
This configuration ensures that the `BAR` variable can only be used if `FOO`
has been assigned a non-empty value. Attempting to build without setting `FOO`
will trigger the validation error.
## Escape variable interpolation
If you want to bypass variable interpolation when parsing the Bake definition,
use double dollar signs (`$${VARIABLE}`).
```hcl {title=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"]
}
```
```console
$ 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
...
```
## 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`.
```hcl {title=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.
```hcl {title=docker-bake.hcl}
variable "BASE_LATEST" {
default = "${BASE_IMAGE}:latest"
}
target "webapp" {
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`.
```console
$ docker buildx bake -f vars.hcl -f docker-bake.hcl --print app
```
```json
{
"target": {
"webapp": {
"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:
- You can override `variable` values using environment variables. See
[Overriding configurations](./overrides.md#environment-variables) for more
information.
- You can refer to and use global variables in functions. See [HCL
functions](./funcs.md#variables-in-functions)
- You can use variable values when evaluating expressions. See [Expression
evaluation](./expressions.md#expressions-with-variables)