Sub topic 13
Build-time Arguments Versus Runtime Environment
This chapter assumes you can read a compose.yaml, know how interpolation and .env files work, and have run docker compose build and up. It covers how variables reach an image build, and why that differs from the container's runtime environment.
Two different moments
A variable can matter at two points in a service's life.
- Build time: Docker runs the Dockerfile instructions to produce an image.
- Runtime: a container starts from that image and runs your program.
The environment and env_file attributes only affect runtime. They are not visible to RUN instructions in a Dockerfile. To pass a value into a build, use build.args, which sets Dockerfile ARG variables.
Passing a build argument
Declare the argument in the Dockerfile with ARG, then supply it under build.args. Interpolation is the usual way to fill it in from .env or the shell.
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim
ARG APP_VERSION=dev
RUN echo "Building version $APP_VERSION"services:
app:
build:
context: .
args:
PYTHON_VERSION: ${PYTHON_VERSION:-3.12}
APP_VERSION: ${APP_VERSION:-dev}
environment:
LOG_LEVEL: infoHere ${APP_VERSION:-dev} is resolved by Compose before the build starts. The result is handed to Docker as the APP_VERSION build argument. LOG_LEVEL is only set when a container runs, so a RUN step cannot read it.
An ARG declared before FROM is available only to the FROM line. To use it after FROM, declare ARG again inside the build stage, as APP_VERSION is above.
Build args do not become runtime variables
An ARG exists only during the build. A running container does not get it as an environment variable. If the program needs the value at runtime, pass it separately, or copy it in the Dockerfile with ENV.
ARG APP_VERSION=dev
ENV APP_VERSION=$APP_VERSIONKeep in mind that values for ARG and ENV are stored in the image metadata. Anyone who can pull the image can read them with docker history or docker inspect.
Comparing the two
| Row | Build args | Runtime environment |
|---|---|---|
| Compose attribute | `build.args` | `environment`, `env_file` |
| Dockerfile keyword | `ARG` | `ENV` (defaults only) |
| Available during `RUN` | Yes | No |
| Available in running container | No, unless copied with `ENV` | Yes |
| Changing it requires | A rebuild | A container recreate |
A changed build arg invalidates the build cache from the first instruction that uses it. Changing a runtime variable does not rebuild anything. docker compose up recreates the container when its configuration changes.
Seeing what will be used
Run docker compose config to confirm that interpolation filled the args as you expect.
APP_VERSION=2.1.0 docker compose configThe output shows build.args with APP_VERSION: 2.1.0. To override an arg for a single build, set the variable in the shell, then rebuild:
APP_VERSION=2.1.0 docker compose build appYou can also give an arg with no value, such as APP_VERSION: under args. Compose then takes the value from the shell where it runs.
Common mistakes
- Setting a variable only under
environmentand reading it in aRUNstep. The step sees an empty value. Move it tobuild.args. - Forgetting the
ARGline. Compose passes the argument, but the Dockerfile ignores it, and Docker may warn that the argument was not consumed. - Expecting a runtime change to alter the image. Editing
environmentdoes not rebuild. Editingbuild.argsneedsdocker compose build, orup --build.
Exercise
A Dockerfile contains RUN echo "Region is $REGION" and prints an empty value. The Compose file sets REGION: eu-west-1 under environment. What two changes fix it?
Show answer
Add ARG REGION to the Dockerfile before the RUN line, and set REGION: eu-west-1 (or ${REGION:-eu-west-1}) under build.args. The environment attribute only applies when the container runs, so the RUN step during the build cannot see it. Then rebuild with docker compose build.