Sub topic 9
Using secret mounts in a RUN step
This chapter assumes you can write a multi-stage Dockerfile, run docker build, and have read the earlier chapters on RUN --mount syntax and on how ARG, ENV and COPY leak secrets into layers. Here you will use type=secret to give one build step a credential without storing it in the image.
How a secret mount works
A secret mount makes a file available to a single RUN instruction. BuildKit places the file in the build container's filesystem, the command runs, and then the file is gone. The content is never written to an image layer, and it does not appear in docker history.
The mount belongs to that one RUN. A later RUN instruction does not see it unless it declares its own mount.
Basic syntax
The mount takes an id and, optionally, a target. The id is the name you pick on the command line. The target is the path where the file appears inside the container.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
--mount=type=cache,target=/root/.npm \
npm ci
COPY . .Here npm ci finds /root/.npmrc and authenticates to the private registry. After the step finishes, the file is not in the layer. The cache mount in the same RUN keeps the npm cache between builds, which shows that mount types combine freely.
Mount options
| Option | Meaning | Default |
|---|---|---|
| `id` | Name that links the mount to a `--secret` flag | The basename of `target` |
| `target` | Path of the file inside the container | `/run/secrets/<id>` |
| `env` | Expose the secret as an environment variable instead of a file | Not set |
| `required` | Fail the build if the secret was not supplied | `false` |
| `mode` | File permissions as an octal number | `0400` |
| `uid`, `gid` | Owner of the file | `0` (root) |
If you omit target, the secret appears at /run/secrets/<id>. Your command can read it from there.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN --mount=type=secret,id=api_token,required=true \
test -s /run/secrets/api_token && echo "token present"Setting required=true makes a missing secret an immediate error. Without it, the mount is silently absent and a later command fails with a less helpful message.
Using a secret as an environment variable
Use env when a tool reads a variable and not a file.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
RUN --mount=type=secret,id=API_TOKEN,env=API_TOKEN \
python -c "import os; print('length:', len(os.environ['API_TOKEN']))"The variable exists only for that command. It is not part of the image's ENV metadata, so a container started from the image does not have it.
Non-root users and permissions
The mounted file is owned by root and readable only by its owner. If the RUN step switches to another user with a USER instruction, that user cannot read the file. Set uid and gid, or relax mode:
# syntax=docker/dockerfile:1
FROM node:20-slim
USER node
RUN --mount=type=secret,id=npmrc,target=/home/node/.npmrc,uid=1000,gid=1000 \
npm whoamiCommon errors
- Secret not found. The
idin the Dockerfile must match theidin--secret. A typo gives an empty or missing file. - Permission denied. The step runs as a non-root user and the file is root-owned. Set
uid. - Secret copied elsewhere. If a command writes the secret into the filesystem, for example with
cporecho, it ends up in the layer. Only the mount itself is protected. - Secret printed in logs. Build output is not scrubbed. Do not
catthe file or enable verbose tool logging.
Exercise
A Dockerfile contains RUN --mount=type=secret,id=db_pass ./migrate.sh, and the script reads /run/secrets/db_pass. The build runs as root and the script reports the file is missing. What is the most likely cause, and how can you make the build fail clearly instead?
Show answer
The build was most likely started without --secret id=db_pass,..., or with a different id, so BuildKit mounted nothing. Add required=true to the mount so the build stops with an error when the secret is not supplied.