Build arguments and environment variables
ARG is for the build and ENV is for the container. Scope rules, how ENV overrides ARG, and why an ARG secret ends up in docker history anyway.
Dockerfile instructions, from FROM to CMDLayer caching and instruction order
Build arguments and environment variables
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- ARG defines a build-time variable set with --build-arg. It is not stored in the image's environment and is gone when the container runs.
- ENV sets a variable for the rest of the build and for every container started from the image. docker run -e overrides it.
- An ARG before the first FROM is global and usable only in FROM lines. Inside a stage, declare it again with a bare ARG NAME to use it.
- A stage inherits ARGs and ENVs from the stage it is built FROM, but unrelated stages each need their own ARG line.
- ENV beats an ARG with the same name. ENV VERSION=${VERSION:-dev} turns a build argument into a runtime variable with a default.
- Build argument values are written into the image history, so anyone who pulls the image can read them. Pass secrets with --secret instead.
- BuildKit predefines TARGETPLATFORM, TARGETARCH, BUILDPLATFORM and friends, plus proxy variables that are kept out of the history unless you declare them.
Two moments, two kinds of variable
An image is built once and run many times. Some values belong to the first moment: which Python version to start from, which commit is being built, which package mirror to use. Others belong to the second: the database URL, the log level, the port. Mixing them up causes two kinds of trouble. Build values baked into the runtime environment leak and go stale. Runtime values baked in at build time force a rebuild for every environment.
Docker gives each moment its own instruction. ARG is for the build. ENV is for the build and the container. Runtime configuration through docker run -e and env files is its own lesson. This one is about what the Dockerfile does with them.
ARG and its scope
# syntax=docker/dockerfile:1
ARG PY_VERSION=3.14
FROM python:${PY_VERSION}-slim
ARG APP_VERSION=dev
ARG PIP_INDEX_URL
ENV APP_VERSION=${APP_VERSION}
LABEL org.opencontainers.image.version=${APP_VERSION}
RUN echo "building $APP_VERSION" > /build-info
CMD ["python", "-c", "import os; print(os.environ['APP_VERSION'])"]
docker build --build-arg APP_VERSION=1.4.2 -t notes:1.4.2 .
The scope rules, from the Dockerfile reference:
- An
ARGbefore the firstFROMis global. It can be used inFROMlines and nowhere else until a stage re-declares it with a bareARG PY_VERSION. - Inside a stage, an
ARGis in effect from its line onwards. Before that line the variable is empty. - A stage built
FROManother stage inherits itsARGs andENVs. Unrelated stages do not, so each needs its ownARGline. - An unset
ARGwith no default is an empty string. BuildKit’sUndefinedVarcheck warns when you reference something never declared.
ENV, and how it meets ARG
ENV is saved in the image config. Every later build step sees it, and so does every container, where docker run -e or Compose can override it. The reference adds one ordering rule: an ENV always overrides an ARG with the same name. That gives a useful idiom, a build argument with a default that becomes a runtime variable:
ARG APP_VERSION
ENV APP_VERSION=${APP_VERSION:-dev}
Use ENV only for values a container really needs. The reference’s own warning is ENV DEBIAN_FRONTEND=noninteractive, which silently changes how apt-get behaves for everyone who later uses your image. If a value is only for one command, put it on that command, RUN DEBIAN_FRONTEND=noninteractive apt-get install ..., or make it an ARG.
Where the values end up
To see what the image records, I converted the Dockerfile above into an image config without building it, using BuildKit v0.35.1’s Dockerfile frontend as a Go library, with APP_VERSION=1.4.2 and a PIP_INDEX_URL carrying credentials. The base image config came from Docker Hub. The parts of the result that came from this Dockerfile:
Env: ..., PYTHON_VERSION=3.14.10, ..., APP_VERSION=1.4.2
Labels: org.opencontainers.image.version=1.4.2
history: ARG APP_VERSION=1.4.2
history: ARG PIP_INDEX_URL=https://user:s3cret@pypi.internal/simple
history: ENV APP_VERSION=1.4.2
history: LABEL org.opencontainers.image.version=1.4.2
history: RUN |2 APP_VERSION=1.4.2 PIP_INDEX_URL=https://user:s3cret@pypi.internal/simple /bin/sh -c echo "building $APP_VERSION" > /build-info
PIP_INDEX_URL is not in Env, as promised. It is in the history twice: once on its own ARG line, and again as the |2 ... prefix on the RUN step, which records the build arguments that step could see. History ships with the image, and docker history --no-trunc prints it. The Dockerfile reference warns about exactly this and adds that build arguments also appear in max-mode provenance attestations.
The rule that follows: never put a secret in an ARG or ENV. Use a secret mount, which reaches the one RUN that needs it and is never written anywhere. BuildKit’s SecretsUsedInArgOrEnv check flags names that look like secrets, such as API_TOKEN or DB_PASSWORD.
Built-in arguments
BuildKit predefines a few ARGs you never declare at the global scope:
TARGETPLATFORM,TARGETOS,TARGETARCH,TARGETVARIANT: the platform being built for.BUILDPLATFORM,BUILDOS,BUILDARCH,BUILDVARIANT: the platform doing the building.
Like any global ARG, they need a bare ARG TARGETARCH inside a stage before a RUN can use them. They are what makes cross-compilation work in multi-platform builds.
The proxy variables, HTTP_PROXY, HTTPS_PROXY, NO_PROXY and their lower-case forms, are predefined too. The reference says they are left out of docker history and the cache key unless you declare them with ARG, so a proxy password passed with --build-arg is not recorded by default.
Cache effects
An ARG affects the cache where it is used, and every RUN below it uses it implicitly as an environment variable. The layer caching lesson shows a changing GIT_SHA busting only the steps below its ARG line. Declare fast-changing arguments as late as you can.
About the video
The embed is Mayank’s “Docker Environment Variables Full Guide: Build Time vs Runtime variables (ARG, ENV, Secrets)”, August 2026, 14.5 minutes. Its chapters cover the build-time and runtime split, ARG compared with ENV, ARG scope, ENV scope, using both in Docker Compose, how docker history remembers everything, BuildKit secret mounts, and runtime secrets. It is from a small channel, and YouTube reports no original-audio language, so English was judged from the title, the description and the English caption track. The DockerCon chapter in deeper is shorter and from a Docker documentation writer.
What was run: the config and history above are real output from a Go program calling dockerfile2llb.Dockerfile2LLB in BuildKit v0.35.1 on the example Dockerfile, with the python:3.14-slim config fetched from Docker Hub. Nothing was built, so no layer was produced. The scope, override and predefined-argument rules are from the Dockerfile reference, checked 2026-11-28.
Where this fits
That finishes what goes into a Dockerfile. Next, catching mistakes in one before they ship: linting Dockerfiles and build checks.
Check yourself
Your Dockerfile starts with ARG PY=3.14, then FROM python:${PY}-slim, then RUN echo $PY. The echo prints an empty line. Why?
An ARG before FROM is in the global scope and only usable in FROM lines. Add a bare ARG PY inside the stage to bring its value in.
You build with --build-arg NPM_TOKEN=... and never use ENV. A colleague runs docker history --no-trunc on the pushed image and sees the token. How?
Every RUN after the ARG line records the build arguments it saw in its history entry, as a prefix such as |1 NPM_TOKEN=.... Use RUN --mount=type=secret so the value never enters the history or a layer.
The Dockerfile has ARG APP_ENV then ENV APP_ENV=production. What does RUN echo $APP_ENV print when built with --build-arg APP_ENV=staging?
production. An ENV with the same name always overrides the ARG from that point on.
How do you make a version passed at build time available to the running app?
Copy it into an ENV, for example ARG APP_VERSION=dev followed by ENV APP_VERSION=$APP_VERSION. The ENV is saved in the image config and every container sees it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.