Docker build variables: ARG and ENV in a Dockerfile
Answer: Build arguments and environment variables both parameterize a Docker build, but they are not a safe way to pass secrets, and they serve different roles.
What they share
In Docker Build, ARG and ENV both pass information into the build so you can parameterize it.
Do not use build arguments or environment variables for secrets, because they're exposed in the final image.
Use secret mounts or SSH mounts when you need to expose secrets securely.
Both forms are declared in the Dockerfile and can be set with flags on docker build.
How ARG and ENV differ
Build arguments are variables for the Dockerfile itself and parameterize instruction values, such as a dependency version.
Build arguments have no effect on the build unless used in an instruction.
Those values are not present in containers from the image unless the Dockerfile passes them through, but they may remain in image metadata, provenance attestations, and image history.
Environment variables are passed into the build execution environment and persist in containers created from the image.
You cannot override or set an environment variable at build-time; the values must be declared in the Dockerfile.
You can combine the two so an environment variable can still be configured at build-time.
See also
Claims
- id: C1 claim: Build arguments and environment variables are inappropriate for passing secrets because they are exposed in the final image. source: S1 evidence: "inappropriate for passing secrets to your build, because they're exposed in the final image"
- id: C2 claim: Build arguments are variables for the Dockerfile itself. source: S1 evidence: "Build arguments are variables for the Dockerfile itself"
- id: C3 claim: Environment variables persist in containers and cannot be set at build-time except through the Dockerfile. source: S2 evidence: "You can't override or set an environment variable at build-time"
Sources
- [S1] Build arguments and environment variables. Docker, Inc.. https://docs.docker.com/build/building/variables/ . License: Apache License 2.0. Accessed 2026-11-26.
- [S2] Build arguments and environment variables (source). Docker, Inc.. https://raw.githubusercontent.com/docker/docs/main/content/manuals/build/building/variables.md . License: Apache License 2.0. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker, Inc., Build arguments and environment variables; Docker, Inc., Build arguments and environment variables (source). Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker, Inc., Build arguments and environment variables; Docker, Inc., Build arguments and environment variables (source). Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker build variables: ARG and ENV in a Dockerfile." iinventedlight Library, iil:note:h99qzhew, version of 2026-11-26. https://iinventedlight.com/library/notes/docker-build-variables/. License: CC-BY-4.0.
@misc{iil_note_h99qzhew,
author = {{iinventedlight}},
title = {{Docker build variables: ARG and ENV in a Dockerfile}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/docker-build-variables/},
note = {Permanent ID iil:note:h99qzhew. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- docker-build-variables.schema.json (schema; CC-BY-4.0)
- docker-build-variables.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-build-variables.md.