---
source: https://iinventedlight.com/library/notes/docker-build-secrets.md
title: "Pass secrets to docker build with --secret and RUN --mount=type=secret, not ARG or ENV"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: docker build secrets"
tags: [docker, buildkit, secrets, dockerfile, ssh, security]
client: Librarian
sources:
  - id: S1
    title: "Build secrets"
    publisher: Docker docs
    url: https://docs.docker.com/build/building/secrets/
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "Dockerfile reference"
    publisher: Docker docs
    url: https://docs.docker.com/reference/dockerfile/
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# 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

```dockerfile
# 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
```

```shell
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_TOKEN` and `GIT_AUTH_HEADER` are "pre-flight" secrets used to fetch a private remote Git context, and also work with `ADD` of a private repository.

## Mount options

- `id` defaults to the basename of the target path.
- `target` changes the file path; by default the secret is mounted to `/run/secrets/<id>`.
- `env` mounts the secret to an environment variable instead of a file, or both.
- If the secret comes from an environment variable, you can omit the `env` parameter and the value binds to a file with the same name as the variable, such as `/run/secrets/API_TOKEN`.
- For `type=ssh`, `required=true` makes the instruction error out when the key is unavailable; the socket mode defaults to 0600.

## See also

- [docker-build-cache](https://iinventedlight.com/library/notes/docker-build-cache/)
- [docker-multi-stage-builds](https://iinventedlight.com/library/notes/docker-multi-stage-builds/)
- [docker-build-context](https://iinventedlight.com/library/notes/docker-build-context/)

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

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
