Sub topic 14
Keeping secrets out of layers
This chapter assumes you know how image layers work, how multi-stage builds separate a build stage from a final stage, and how to run docker build and docker history. It covers how secrets leak into images and how to stop that from happening.
Why layers keep secrets
Every COPY, ADD and RUN instruction creates a layer, and layers are read-only. If a step writes a secret into a layer, a later step can hide the file but cannot remove it from the layer that holds it. Anyone who pulls the image can unpack that layer and read the secret.
Three common mistakes cause this:
COPY .env .orCOPY . .without a.dockerignore, which puts credential files in a layer.ARG TOKENorENV TOKEN=...used for a credential. Build arguments and environment values are recorded in the image metadata, anddocker historyanddocker inspectcan show them.RUN echo "$TOKEN" > ~/.npmrc && npm ci && rm ~/.npmrc. Thermadds a new layer on top, and the earlier layer still holds the file.
FROM node:20
ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
RUN npm ci
RUN rm .npmrcThis Dockerfile is unsafe. The token is stored in the ARG metadata and in the layer that wrote .npmrc. Deleting the file in the last step hides it from the running container but not from the image.
Using multi-stage builds is not enough
A multi-stage build keeps build-stage layers out of the final image, so a secret used only in the build stage does not ship in the final image. This helps, but it has limits:
- The build stage still exists in your local cache and in any CI cache that stores it.
- If you push the build stage (for example with
--target build), the secret goes with it. - Anything you copy from the build stage into the final stage, such as a whole directory, can carry a secret along.
Treat multi-stage builds as a second layer of protection. The first layer is never writing the secret into a layer.
Build secrets with BuildKit
BuildKit can mount a secret as a temporary file during a single RUN instruction. The file exists only for that step and is not stored in any layer.
# syntax=docker/dockerfile:1
FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
COPY . .
RUN npm run buildPass the secret from a file on the host when you build:
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp:small .The id links the --secret flag to the --mount in the Dockerfile. By default the secret appears at /run/secrets/<id>. The target option places it where the tool expects to find it, here /root/.npmrc.
You can also read a secret from an environment variable on the host:
export NPM_TOKEN=example-value
docker build --secret id=NPM_TOKEN,env=NPM_TOKEN -t myapp:small .For private Git repositories, use an SSH mount instead of copying keys: RUN --mount=type=ssh git clone [email protected]:example/private.git, then build with docker build --ssh default ..
Check your image
Look for leaks before you publish:
docker history --no-trunc myapp:small
docker inspect myapp:small --format '{{json .Config.Env}}'The first command shows the full text of every instruction, including any inline values. The second lists environment variables stored in the image. Neither should contain a credential.
If a secret has already been in a pushed image, rotate it. Deleting the image from a registry does not undo copies that others may have pulled.
Exercise
A Dockerfile has RUN echo "$TOKEN" > .npmrc && npm ci && rm .npmrc, where TOKEN comes from an ARG. The final container has no .npmrc. Why is the token still exposed, and how would you fix it?
Show answer
The RUN step writes .npmrc into a layer, and the ARG value is recorded in the image history. Removing the file in the same step does not remove the value from the build metadata, and if rm is in a later step the file stays in the earlier layer. Fix it by using RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci and building with docker build --secret id=npmrc,src=$HOME/.npmrc ., so the secret is never written to a layer or stored as an argument.