← All notes

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

Mount options

See also

Claims

Sources

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

Permanent ID: iil:note:gzj6kpwf

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.},
}

Markdown

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.

All artifact files (JSON index)

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.