All guides

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.

Code in an editor on a laptop screen

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. Each ENV line 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, an ARG variable stays out of the image and the final container cannot read it. The same section warns that docker history shows build arguments, and so do provenance attestations in max mode.
  • COPY and a later rm. 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:

Shell

docker build --secret id=aws,src=$HOME/.aws/credentials .

Dockerfile

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>. target sets a different path.
  • The source can be a file or an environment variable. --secret id=kube,env=KUBECONFIG mounts the KUBECONFIG variable as secret ID kube. --secret id=API_TOKEN mounts the value of the API_TOKEN variable at /run/secrets/API_TOKEN.
  • In the Dockerfile, env mounts the secret as an environment variable in place of a file. The option exists since Dockerfile v1.10.0.
  • required=true makes the instruction error out when the secret is unavailable. The default is false.
  • The file mode defaults to 0400, and the owner to UID 0 and GID 0. When a non-root user runs the RUN instruction, give the mount a uid, gid or mode that 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:

Dockerfile

# 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 /hello

Shell

eval $(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:

  • secrets takes an inline value from the workflow, as id=value.
  • secret-envs takes an environment variable on the runner, as id=VARIABLE.
  • secret-files takes a file on the runner, as id=path.
  • ssh takes an SSH agent socket or keys, as in ssh: default.

Docker's example for a private npm registry creates an .npmrc file in the workflow and mounts it as a secret:

YAML

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

Dockerfile

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

user/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

  1. Keeping a credential in use after it went through ARG or ENV. It persists in the image. Rotate it, then rebuild with a secret mount.
  2. 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.
  3. Leaving required at false for a secret the step depends on. Set required=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.

Book a call

More guides