Sub topic 7
Keeping Secrets Out of Images and Environment Variables
This chapter assumes you can write a Dockerfile, run containers, and read docker history output. It covers how secrets leak into images and how to pass them safely at build time and run time.
Why secrets leak
An image is a stack of layers, and anyone who can pull the image can read every layer. A secret that touches a layer stays in it, even if a later step deletes the file.
The common leaks are:
COPY .env .orCOPY id_rsa /root/.ssh/followed byRUN rm. The earlier layer still holds the file.ARG NPM_TOKENused in aRUNstep. Build arguments are recorded indocker historyand in image metadata.ENV API_KEY=...in the Dockerfile. The value is baked into the image config.- Secrets in
docker run -e. They appear indocker inspect, in/proc/<pid>/environ, and often in crash dumps and logs.
Build-time secrets: BuildKit secret mounts
A BuildKit secret mount exposes a file to one RUN step only. It is not written to the layer.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN="$(cat /run/secrets/npm_token)" npm ci
COPY . .
USER node
CMD ["node", "server.js"]The id links the mount to the value you supply on the command line. The file appears at /run/secrets/<id> during that step.
docker build --secret id=npm_token,src=./npm_token.txt -t myapp:1.0 .Keep npm_token.txt out of the build context. Add it to .dockerignore so a later COPY . . cannot pick it up.
Run-time secrets
Environment variables are convenient, but they leak easily. Child processes inherit them, and many tools print them on error. Prefer files that the container reads at start-up.
| Method | Where the value lives | Main risk |
|---|---|---|
| `-e KEY=value` | Container config, process environment | Shown by `docker inspect`, inherited by children |
| `--env-file` | Same as `-e`, plus the file on disk | File permissions, shell history avoided |
| Bind-mounted file | Host file, read-only in container | Host file permissions |
| Docker Swarm or Compose secret | `/run/secrets/<name>` in a memory-backed mount | Needs Swarm or Compose configuration |
| External secret manager | Fetched by the app at start-up | Needs an identity to authenticate |
A Compose file can mount a secret as a file:
services:
web:
image: myapp:1.0
secrets:
- db_password
environment:
DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
db_password:
file: ./db_password.txtThe application reads the path in DB_PASSWORD_FILE instead of holding the password in its environment. Many official images, such as Postgres and MySQL, support this _FILE convention.
Checking your work
Look for leaks before you push:
- Run
docker history --no-trunc myapp:1.0and look for tokens in commands. - Run
docker inspect myapp:1.0and read theEnvsection. - Run a secret scanner such as Trivy (
trivy image --scanners secret myapp:1.0) against the image.
Exercise
A Dockerfile has ARG NPM_TOKEN, then RUN npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN && npm ci. A teammate says it is safe because the token is not in an ENV. Is that right, and what should you change?
Show answer
It is not safe. Build arguments are recorded in docker history, and the npm config set command writes the token to an .npmrc file in a layer. Switch to a BuildKit secret mount (RUN --mount=type=secret,id=npm_token ...), pass the value with docker build --secret, and rotate the token that was already exposed.
Exercise
Why is mounting a secret as a file at /run/secrets/db_password safer than docker run -e DB_PASSWORD=...?
Show answer
The value does not appear in docker inspect output or in the process environment, so child processes and error dumps do not inherit it. Access is controlled by file permissions inside the container.