Sub topic 9
Build-time values with ARG and --build-arg
This chapter assumes you can write a basic Dockerfile, run docker build with a context and -t, and know that each instruction creates a layer. Here you will pass values into a build and learn how ARG differs from ENV.
What ARG does
ARG defines a variable that exists only while the image is being built. You give it a default in the Dockerfile. You can override that default with --build-arg on the command line.
ENV sets an environment variable that is stored in the image and is present when a container runs. The usual pattern is to receive a value with ARG and copy it into ENV when the running container also needs it.
FROM node:20-slim
ARG APP_VERSION=dev
ENV APP_VERSION=$APP_VERSION
WORKDIR /app
COPY . .
CMD ["node", "server.js"]If you build without any flags, APP_VERSION is dev. To override it, pass --build-arg:
docker build --build-arg APP_VERSION=1.2.3 -t myapp:1.2.3 .Now the image contains APP_VERSION=1.2.3 as an environment variable, and the tag matches it.
ARG versus ENV
| Property | ARG | ENV |
|---|---|---|
| Available during the build | Yes, after its declaration | Yes, after its declaration |
| Available in a running container | No | Yes |
| Set from the command line | `--build-arg NAME=value` | Not at build time. Use `docker run -e` at run time. |
| Stored in the image | The name and value can appear in `docker history` | Yes, in the image configuration |
An ARG value is not a container environment variable. In the example above, the running container sees APP_VERSION only because ENV copied it.
Scope and placement
An ARG applies from the line where it is declared to the end of the build stage that contains it. Two rules follow from this.
- An
ARGdeclared before the firstFROMcan be used in theFROMline, but it is not visible inside the stage. To use it there, declareARG NAMEagain afterFROM. - Each stage in a multi-stage build needs its own
ARGdeclaration.
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim
ARG APP_VERSION=dev
RUN echo "Building version $APP_VERSION"Here NODE_VERSION selects the base image. APP_VERSION is declared after FROM, so the RUN instruction can read it.
ARG and the build cache
A changed ARG value invalidates the cache. Instructions that use the value, and every instruction after them, are rebuilt. Place ARG as late as you can, just before the first instruction that needs it. A declaration at the top that changes on every build, such as a commit hash, forces a full rebuild of everything below it.
Common errors
- Passing an argument the Dockerfile does not declare. Docker warns that the build argument was not consumed. The value has no effect. Check the spelling of the name.
- Expecting an ARG at run time. Running
printenv APP_VERSIONin the container prints nothing unlessENVcopied the value. - Putting secrets in ARG. The value is recorded in the image history and can be read by anyone who has the image. Use build secrets for credentials, covered in a later chapter.
- Declaring ARG before FROM and using it later. It is out of scope after
FROMuntil you redeclare it.
Try it
docker build --build-arg APP_VERSION=1.2.3 -t myapp:1.2.3 .
docker run --rm myapp:1.2.3 printenv APP_VERSION
docker history myapp:1.2.3The second command prints 1.2.3. The history output shows the ENV instruction with the value that was set.
Exercise
A Dockerfile contains ARG BUILD_DATE and a RUN instruction that uses it, but no ENV line. You build with --build-arg BUILD_DATE=2026-12-04 and then run printenv BUILD_DATE in a container from the image. What does it print, and how would you make the value available at run time?
Show answer
It prints nothing, because ARG values exist only during the build. Add ENV BUILD_DATE=$BUILD_DATE after the ARG line so the value is stored in the image and set in the container.