Pass secrets to docker build with --secret and RUN --mount=type=secret, not ARG or ENV
Answer: Pass the secret with docker build --secret id=aws,src=$HOME/.aws/credentials . and consume it with RUN --mount=type=secret,id=aws ...; it is mounted at /run/secrets/aws only for that instruction. Build arguments and environment variables are inappropriate for secrets because they persist in the final image.
Example
# syntax=docker/dockerfile:1
FROM alpine
RUN --mount=type=secret,id=aws \
AWS_SHARED_CREDENTIALS_FILE=/run/secrets/aws \
aws s3 cp ...
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials aws s3 ls
RUN --mount=type=secret,id=API_TOKEN,env=API_TOKEN ./fetch.sh
RUN --mount=type=ssh git clone git@gitlab.com:org/private.git
docker build --secret id=aws,src=$HOME/.aws/credentials .
docker build --secret id=API_TOKEN .
docker build --ssh default .
Types of build secrets
- Secret mounts make a secret from the build client temporarily available inside the build container for the duration of the build instruction, as files or environment variables.
- SSH mounts make SSH sockets or keys available inside builds; they're commonly used to fetch private Git repositories.
- Git authentication secrets
GIT_AUTH_TOKENandGIT_AUTH_HEADERare "pre-flight" secrets used to fetch a private remote Git context, and also work withADDof a private repository.
Mount options
iddefaults to the basename of the target path.targetchanges the file path; by default the secret is mounted to/run/secrets/<id>.envmounts the secret to an environment variable instead of a file, or both.- If the secret comes from an environment variable, you can omit the
envparameter and the value binds to a file with the same name as the variable, such as/run/secrets/API_TOKEN. - For
type=ssh,required=truemakes the instruction error out when the key is unavailable; the socket mode defaults to 0600.
See also
Claims
- id: C1 claim: Build args and env vars persist in the final image. source: S1 evidence: "because they persist in the final image"
- id: C2 claim: The default secret path is /run/secrets/<id>. source: S1 evidence: "The default file path of the secret, inside the build container, is /run/secrets/<id> ."
- id: C3 claim: Secret mounts avoid baking values into the image. source: S2 evidence: "without baking them into the image"
Sources
- [S1] Build secrets. Docker docs. https://docs.docker.com/build/building/secrets/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] Dockerfile reference. Docker docs. https://docs.docker.com/reference/dockerfile/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Build secrets; Docker docs, Dockerfile reference. 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 docs, Build secrets; Docker docs, Dockerfile reference. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Pass secrets to docker build with --secret and RUN --mount=type=secret, not ARG or ENV." iinventedlight Library, iil:note:gzj6kpwf, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-build-secrets/. License: CC-BY-4.0.
@misc{iil_note_gzj6kpwf,
author = {{iinventedlight}},
title = {{Pass secrets to docker build with --secret and RUN --mount=type=secret, not ARG or ENV}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-build-secrets/},
note = {Permanent ID iil:note:gzj6kpwf. 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-secrets.table.json (table; CC-BY-4.0)
- docker-build-secrets.schema.json (schema; 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-secrets.md.