Difficulty: Intermediate
What is the difference between ARG and ENV in a Dockerfile, and when should you use each?
ARG defines a build-time variable: it is set with docker build --build-arg, can be used by later instructions during the build, and is not present in the environment of the running container. ENV sets an environment variable that is used during the build and stored in the image, so every container started from it sees the value, and it can be overridden at runtime with docker run -e. Use ARG for things that change how the image is built, and ENV for configuration the running app reads.
ARG | ENV | |
|---|---|---|
| Set with | --build-arg NAME=value or a default in the Dockerfile | The Dockerfile; overridden by -e / --env-file at run time |
| Available during build | Yes, after it is declared | Yes, after it is declared |
| Available in the running container | No | Yes |
| Stored in the image config | No, though values used in RUN can show up in docker history | Yes, visible in docker inspect |
| Typical use | Base image version, package versions, target platform | NODE_ENV, PORT, PATH additions |
ARG declared before FROM can only be used in FROM lines. To use it inside the stage, declare it again (ARG NODE_VERSION without a value) after FROM.ARG or ENV in the builder stage does not exist in the final stage.ARG and an ENV share a name, the ENV value wins for later instructions.ARG as late as you can to keep earlier layers cached.A common pattern is to accept a value at build time and bake it into the environment: ARG APP_VERSION then ENV APP_VERSION=$APP_VERSION. CI passes the Git SHA as a build arg, and the running app can report its version on a /health endpoint.
ENV values are stored in the image and readable by anyone who can pull it.ARG values used in a RUN can be recovered from image history and build provenance. BuildKit's build checks warn about this (SecretsUsedInArgOrEnv).RUN --mount=type=secret,id=npm_token ... with docker build --secret id=npm_token,env=NPM_TOKEN. The secret is available only to that one RUN and is never written to a layer.--env-file, Docker or Compose secrets, or your orchestrator's secret store, never in the Dockerfile.BuildKit also provides automatic platform args such as TARGETPLATFORM, TARGETOS and TARGETARCH, which you declare with ARG TARGETARCH and use to download the right binary in multi-architecture builds.
# Dockerfile
ARG NODE_VERSION=22
FROM node:${NODE_VERSION}-alpine
# Re-declare to use a pre-FROM ARG inside the stage
ARG NODE_VERSION
ARG APP_VERSION=dev
# Baked into the image and visible at runtime
ENV NODE_ENV=production \
APP_VERSION=${APP_VERSION}
RUN echo "Building on Node $NODE_VERSION, version $APP_VERSION"
COPY . /app
CMD ["node", "/app/server.js"]
# Build: docker build --build-arg APP_VERSION=$(git rev-parse --short HEAD) -t api .
# Run: docker run -e NODE_ENV=staging api
NODE_VERSION picks the base image at build time only, APP_VERSION is carried into the runtime environment, and NODE_ENV can be overridden per container.
# Dockerfile
# RUN --mount=type=secret,id=npm_token,env=NPM_TOKEN npm ci
# Build command: the token comes from your shell env
docker build --secret id=npm_token,env=NPM_TOKEN -t api .
The secret mount exposes the token only to that RUN step; it never appears in a layer, the image config or docker history.
ARG, ENV, Build-time variables, Runtime environment, Build secrets