ContainersDocker
Docker build secrets: keep tokens out of image layers
A token passed to a build as a build argument or an environment variable persists in the image. A secret mount makes the value available to one build instruction and keeps it out of the image and the build cache.

Why ARG and ENV leak
Docker's Build secrets page is direct about it: do not pass a secret to a build through a build argument or an environment variable, because both persist in the final image. Three other pages give the details:
ENV. EachENVline creates a layer. Even if a later layer unsets the variable, it still persists in that layer and its value can be dumped.ARG. According to the Dockerfile reference, anARGvariable stays out of the image and the final container cannot read it. The same section warns thatdocker historyshows build arguments, and so do provenance attestations inmaxmode.COPYand a laterrm. Docker's storage page explains that each layer is a set of differences from the layer before it. A directory removed by a later instruction is still available in the previous layer.
The Build secrets page and the Dockerfile reference word the ARG case differently. Both tell you not to use build arguments for secrets.
Secret mounts
Using a build secret is a two-step process. You pass the secret to the docker build command, and you consume it in the Dockerfile. This is the pair from Docker's documentation:
docker build --secret id=aws,src=$HOME/.aws/credentials .RUN --mount=type=secret,id=aws \
AWS_SHARED_CREDENTIALS_FILE=/run/secrets/aws \
aws s3 cp ...A secret mount exposes the secret inside the build container for one build instruction and no longer. The Dockerfile reference describes the mount type as access to secure files without baking them into the image or the build cache.
The documented options:
- By default the secret is a file at
/run/secrets/<id>.targetsets a different path. - The source can be a file or an environment variable.
--secret id=kube,env=KUBECONFIGmounts theKUBECONFIGvariable as secret IDkube.--secret id=API_TOKENmounts the value of theAPI_TOKENvariable at/run/secrets/API_TOKEN. - In the Dockerfile,
envmounts the secret as an environment variable in place of a file. The option exists since Dockerfile v1.10.0. required=truemakes the instruction error out when the secret is unavailable. The default isfalse.- The file mode defaults to
0400, and the owner to UID 0 and GID 0. When a non-root user runs theRUNinstruction, give the mount auid,gidormodethat lets that user read the file.
SSH mounts
An SSH mount gives the build container access to SSH keys through an SSH agent, with support for passphrases. Docker names cloning private Git repositories as a common use case. The example in the Dockerfile reference connects to GitLab:
# syntax=docker/dockerfile:1
FROM alpine
RUN apk add --no-cache openssh-client
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh \
ssh -q -T [email protected] 2>&1 | tee /helloeval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
docker buildx build --ssh default=$SSH_AUTH_SOCK .The example installs an SSH client, adds the host key of gitlab.com, and runs one command with the mount. In place of $SSH_AUTH_SOCK you can give the path to a *.pem file on the host, but pem files with passphrases are not supported.
For Git over HTTP, BuildKit has two pre-defined build secrets, GIT_AUTH_TOKEN and GIT_AUTH_HEADER. They apply when a private Git repository is the build context or is fetched with ADD.
Secrets in GitHub Actions
docker/build-push-action has three inputs for secret mounts and one for SSH:
secretstakes an inline value from the workflow, asid=value.secret-envstakes an environment variable on the runner, asid=VARIABLE.secret-filestakes a file on the runner, asid=path.sshtakes an SSH agent socket or keys, as inssh: default.
Docker's example for a private npm registry creates an .npmrc file in the workflow and mounts it as a secret:
name: ci
on:
push:
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Create .npmrc file
run: |
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
- name: Build
uses: docker/build-push-action@v7
with:
context: .
secret-files: |
npmrc=./.npmrc
tags: user/app:latest# syntax=docker/dockerfile:1
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm ci
COPY . .
RUN npm run builduser/app:latest, the NPM_TOKEN secret and the npmrc ID are the documentation's names. Use your own.
Checking a Dockerfile and an image
docker build --check . runs the build checks without building. It does not execute the build steps, and it exits with a non-zero status code if any violations are reported. One check, SecretsUsedInArgOrEnv, reports ARG and ENV keys that indicate they contain sensitive data.
On an image that already exists, docker history is where build arguments are visible.
Mistakes to avoid
- Keeping a credential in use after it went through
ARGorENV. It persists in the image. Rotate it, then rebuild with a secret mount. - Expecting a new secret value to rerun a step. Changing the value of a secret does not invalidate the cache. Docker's page suggests a build argument that you change together with the secret, and its example names the argument
CACHEBUST. - Leaving
requiredatfalsefor a secret the step depends on. Setrequired=true, so the instruction errors out when the secret is unavailable.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Secrets in CI/CD: where they leak and how to stop it
Secrets · 4 min read
Six places a pipeline secret escapes, and the GitHub Actions settings that close each one.

A secret leaked to Git: what to do in the first hour
Secrets · 4 min read
Revoke first, read the logs, then clean the history, and know what a force push leaves behind.

Docker build cache: order the layers and reuse them in CI
Containers · 4 min read
Dependencies before source, cache mounts for package managers, and an external cache for CI builders.