ARG vs. ENV

Difficulty: Intermediate

Question

What is the difference between ARG and ENV in a Dockerfile, and when should you use each?

Answer

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.

Side by side

ARGENV
Set with--build-arg NAME=value or a default in the DockerfileThe Dockerfile; overridden by -e / --env-file at run time
Available during buildYes, after it is declaredYes, after it is declared
Available in the running containerNoYes
Stored in the image configNo, though values used in RUN can show up in docker historyYes, visible in docker inspect
Typical useBase image version, package versions, target platformNODE_ENV, PORT, PATH additions

Scope rules that cause real bugs

  • An 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.
  • Each stage of a multi-stage build has its own scope. An ARG or ENV in the builder stage does not exist in the final stage.
  • If an ARG and an ENV share a name, the ENV value wins for later instructions.
  • Changing a build arg's value invalidates the cache from the first instruction that uses it, so declare ARG as late as you can to keep earlier layers cached.

Passing a build value through to runtime

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.

Neither is for secrets

  • 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).
  • For build-time secrets such as a private npm token, use BuildKit secret mounts: 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.
  • For runtime secrets like a Razorpay API key, inject them at run time via --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.

Where ARG and ENV values live. ARG stops at the end of the build; ENV travels with the image into every container.
  1. docker build --build-arg: Supplies ARG values for this build only.
  2. Dockerfile instructions: Both ARG and ENV are usable by RUN.
  3. Image config: Stores ENV values; ARG values are not kept as env.
  4. docker run -e: Can override any ENV value.
  5. Running process environment: Sees ENV values, never plain ARG values.

Code examples

ARG and ENV working together

# 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.

Build secret without leaking it

# 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.

Key points

  • ARG exists only during the build; ENV is stored in the image and present at runtime.
  • ARG before FROM must be re-declared inside the stage to be used there.
  • Each build stage has its own ARG and ENV scope.
  • Neither ARG nor ENV is safe for secrets; use RUN --mount=type=secret for build secrets.
  • Changing an ARG value busts the cache from its first use, so declare ARGs late.

Common follow-up questions

  • How would you pass a private registry token into an npm install during a build?
  • Why is an ARG declared before FROM not visible inside the stage?
  • How do you override an ENV value when starting a container?
  • What are TARGETARCH and TARGETPLATFORM used for?

Interview tips

  • Answer in terms of lifetime: build-time versus build-plus-runtime.
  • Volunteer the secrets point; saying neither is safe and naming secret mounts is a strong signal.
  • Mention the ARG-before-FROM scope rule; it is a classic Dockerfile bug.

Concepts covered

ARG, ENV, Build-time variables, Runtime environment, Build secrets

More Docker & Containers questions