Sub topic 5
Interpolation syntax: $VAR and ${VAR}
This chapter assumes you can read a compose.yaml, set shell variables, and run docker compose up. It also assumes you know that Compose reads a .env file for interpolation values.
What interpolation does
Interpolation is the substitution of variable references in your Compose file with real values. Compose does this when it loads the file, before it creates any container. The container never sees ${APP_TAG}. It only sees the finished text.
Compose looks up each name in the shell environment first, then in the .env file. The precedence chapter covers the full order.
The two forms
You can write a reference in two ways:
$VARis the short form.${VAR}is the braced form.
Both give the same result when the name is followed by something that cannot belong to a name. A variable name contains letters, digits, and underscores, and it cannot start with a digit.
Here is a working example. First the .env file:
REGISTRY=registry.example.com
APP_TAG=1.4.2
WEB_PORT=8080Then the Compose file:
services:
web:
image: ${REGISTRY}/web:${APP_TAG}
ports:
- "${WEB_PORT}:80"
labels:
com.example.release: "${APP_TAG}_blue"With those values, the image becomes registry.example.com/web:1.4.2. The port mapping becomes 8080:80, and the label value becomes 1.4.2_blue.
Why braces matter
Without braces, Compose reads the longest run of name characters after the $. If text that follows your variable is made of letters, digits, or underscores, Compose treats it as part of the name.
This table assumes APP_TAG=1.4.2 and no variable called APP_TAG_blue.
| Written | Name Compose looks up | Result |
|---|---|---|
| `$APP_TAG` | `APP_TAG` | `1.4.2` |
| `${APP_TAG}` | `APP_TAG` | `1.4.2` |
| `$APP_TAG_blue` | `APP_TAG_blue` | empty string |
| `${APP_TAG}_blue` | `APP_TAG` | `1.4.2_blue` |
| `$APP_TAG-blue` | `APP_TAG` | `1.4.2-blue` |
The hyphen ends the name, so $APP_TAG-blue works. The underscore does not, so $APP_TAG_blue silently fails.
Use braces whenever a reference sits next to other text. Many teams use them everywhere, because that style is easy to read and never depends on what follows the name.
Where interpolation applies
Compose substitutes inside values of the file, such as image names, ports, labels, and commands. It does not substitute in keys. You cannot write ${SERVICE_NAME}: as a service name and expect it to expand.
References also work inside quoted strings, as the port mapping above shows. Quoting the port mapping is good practice anyway, because YAML can misread unquoted host:container numbers.
The braced form also supports modifiers that supply defaults or require a value. For example, ${APP_TAG:-latest} falls back to latest. The next chapter covers these in detail. This chapter only needs you to know that braces are what make them possible. The short form $VAR cannot carry a modifier.
Interpolation versus container variables
Interpolation changes the text of the Compose file. It does not, by itself, put a variable inside a container. In the example above, APP_TAG is used to build the image name and a label. The running container has no APP_TAG variable unless you also pass it through environment or env_file.
Mixing these up is a common source of confusion. If a value must appear in the container, pass it explicitly.
Exercise
A .env file contains APP_TAG=2.0. A Compose file has image: my-app:$APP_TAG_stable. Which image name does Compose produce, and how do you fix it so the result is my-app:2.0_stable?
Show answer
Compose reads the variable name as APP_TAG_stable, which is unset. It produces my-app: and prints a warning. The fix is to use braces: image: my-app:${APP_TAG}_stable.