Sub topic 14
Using Build Secrets Safely
This chapter assumes you can write a Dockerfile, run docker build, and know that each instruction creates an image layer. It also assumes you know that BuildKit is the modern build engine behind docker build.
Why secrets in images are a problem
Some builds need a credential. A private package registry token is a common example. The obvious ways to pass one are unsafe:
ARGandENVvalues are recorded in the image metadata. Anyone can read them withdocker historyordocker image inspect.COPY .npmrc /root/.npmrcstores the file in a layer. Deleting it in a laterRUN rmdoes not help, because the earlier layer still contains it.- Images get pushed and shared. A secret baked into any layer travels with the image.
A build secret avoids this. BuildKit mounts the secret as a file for the duration of one RUN instruction only. The file is never written to a layer.
Mounting a secret in a RUN instruction
The first line, # syntax=docker/dockerfile:1, selects the current Dockerfile frontend so the --mount option is available.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
COPY . .The mount has three parts:
type=secretmarks this as a secret mount.id=npmrcis the name the build command uses to supply the secret.target=/root/.npmrcis where the file appears inside the container for that one instruction.
If you omit target, the secret appears at /run/secrets/<id>.
Supplying the secret at build time
Pass the secret with --secret. The id must match the Dockerfile.
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp:1.0 .The src value is a file on your machine. It is not part of the build context, so .dockerignore does not affect it. Recent Docker versions use BuildKit by default. On older installations, set DOCKER_BUILDKIT=1 before the command.
You can also read a secret from an environment variable:
export NPM_TOKEN=example-token
docker build --secret id=npm_token,env=NPM_TOKEN -t myapp:1.0 .# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN --mount=type=secret,id=npm_token \
test -s /run/secrets/npm_token && echo "token is available"Read the value inside the RUN command, for example with cat /run/secrets/npm_token. Do not write it to a file outside the mount or into an ENV instruction.
What happens during the build
Common errors
- The secret is empty or missing. The
idin--mountand--secretmust match exactly. A mismatch gives you an empty mount or an error. - Using the secret outside the
RUNit is mounted on. The mount exists only for that instruction. Each step that needs it must declare its own--mount. - Leaking the value through output. If the command prints the secret, it appears in the build log. Avoid
echoof the value andset -xin those steps. - Copying the result of the secret. If the
RUNstep writes the secret into the image, for examplecat /run/secrets/token > /app/token, it is stored in a layer. Only the mount itself is protected. - Missing syntax line. Without
# syntax=docker/dockerfile:1, older frontends may reject--mount.
Checking your work
After building, confirm nothing leaked. Search the image history and metadata for the secret value:
docker history --no-trunc myapp:1.0
docker image inspect myapp:1.0Neither output should contain the token or the .npmrc content.
Exercise
A Dockerfile contains RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci. The build prints an error saying the file is missing. You ran docker build --secret id=npm,src=$HOME/.npmrc -t myapp .. What is wrong, and why is this approach safer than COPY .npmrc /root/.npmrc followed by RUN rm /root/.npmrc?
Show answer
The ids do not match: the Dockerfile uses npmrc but the command supplies npm. Change the command to --secret id=npmrc,.... The mount is safer because the secret exists only during that RUN instruction and is never written to a layer. With COPY and rm, the file stays in the earlier COPY layer and can be extracted from the image.