Sub topic 10
Passing secrets from files and environment variables
This chapter assumes you can write a multi-stage Dockerfile and run docker build. It also assumes you know the RUN --mount=type=secret syntax from the previous chapter. Here you will learn how to supply the secret on the command line, how the secret id links the command to the Dockerfile, and how to check that nothing leaked.
The secret id connects two places
A secret mount has two sides. The Dockerfile declares an id. The docker build command supplies a value under that same id. If the ids differ, the mount is empty and the step usually fails.
# 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 . .In this Dockerfile the id is npmrc. The target is the path where the file appears inside the build container, and only while that one RUN step executes. If you leave out target, the file appears at /run/secrets/<id>.
Passing a secret from a file
Use src to point at a file on the host.
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp .BuildKit reads the file and makes it available to the step. The file is never copied into a layer.
Passing a secret from an environment variable
Use env to read a variable from the shell that runs the build.
export API_TOKEN=example-value
docker build --secret id=API_TOKEN,env=API_TOKEN -t myapp .Inside the build, the value is exposed as a file at /run/secrets/API_TOKEN. It is not an environment variable by default. If your tool needs an environment variable, read the file in the same RUN command:
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN apk add --no-cache curl
RUN --mount=type=secret,id=API_TOKEN \
API_TOKEN="$(cat /run/secrets/API_TOKEN)" \
&& curl -sf -H "Authorization: Bearer ${API_TOKEN}" https://example.com/private/data -o /tmp/dataThe variable exists only inside that shell command. It is not saved in the image configuration.
You can also give an environment-variable secret an env option on the mount, which exposes it as a variable directly. Check that your Docker version supports it before relying on it.
The two ways to pass a secret
| Option | Source of the value | Typical use |
|---|---|---|
| `src=<path>` | A file on the host | Config files such as `.npmrc` or `.netrc` |
| `env=<NAME>` | A variable in the calling shell | Tokens set by a CI system |
If you omit both, BuildKit looks for an environment variable with the same name as the id.
Verifying that nothing leaked
A secret mount should leave no trace. You can check the image history, which lists the command of every layer.
docker history --no-trunc myappLook through the output for your token value or for .npmrc contents. With a secret mount you should find only the RUN instruction text, which names the id but not the value.
Compare this with passing the token as a build argument. An ARG value is recorded in the history, so anyone who can pull the image can read it.
Common mistakes
- Mismatched ids.
--secret id=npmrcmust matchid=npmrcin the Dockerfile exactly, including case. - Printing the secret. A command such as
echo $TOKENcan appear in build logs. CI logs are often retained for a long time. - Using
--build-argfor convenience. It works, but it stores the value in the image metadata. - Missing
# syntaxline or old Docker. Without BuildKit,--mountis rejected as an unknown flag.
Exercise
You run docker build --secret id=token,src=./token.txt . but the RUN step in the Dockerfile uses --mount=type=secret,id=API_TOKEN and fails because the file is empty. What is wrong and how do you fix it?
Show answer
The ids do not match. The command supplies token while the Dockerfile asks for API_TOKEN. Change one so both use the same id, for example --secret id=API_TOKEN,src=./token.txt.
Exercise
Your build step needs the secret as an environment variable named API_TOKEN for one curl command. How do you supply it without storing it in the image?
Show answer
Pass it with --secret id=API_TOKEN,env=API_TOKEN, mount it with --mount=type=secret,id=API_TOKEN, and set the variable inside the same RUN command with API_TOKEN="$(cat /run/secrets/API_TOKEN)". Then run docker history --no-trunc to confirm the value is absent.