Sub topic 8
How secrets end up in image layers
This chapter assumes you can write a basic Dockerfile, run docker build, and know that each instruction can add a layer to the image. It covers how credentials leak through ARG, ENV, and COPY, and why deleting them later does not help. The next chapters show the fix.
Why an image keeps everything
An image is a stack of layers plus metadata. Each layer is a filesystem diff, and the metadata records the instruction that created it. Anyone who can pull the image can read both. A later RUN rm adds a new layer that hides the file. The earlier layer still holds it, and it can be extracted.
Leak path 1: ARG
ARG values are meant for build-time configuration. They are not private. The value used for each instruction is recorded in the image history.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
ARG NPM_TOKEN
COPY package*.json ./
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc \
&& npm ci \
&& rm .npmrcThis Dockerfile looks careful because it removes .npmrc in the same RUN. It still leaks in two ways:
- The
RUNcommand line, with${NPM_TOKEN}expanded by the build, is stored in the image history. Rundocker history --no-truncon the image to see it. - When the build runs without a cache mount for the file, the layer for that step is committed. Removing the file inside the same step keeps it out of that layer, but the token is still in the history text.
You can check this yourself:
docker build --build-arg NPM_TOKEN=example-value -t leaky .
docker history --no-trunc leaky | grep -i npmIf the token text appears in the output, it is recorded in the image.
Leak path 2: ENV
ENV writes the value into the image configuration. Every container started from the image inherits it, and docker inspect prints it.
# syntax=docker/dockerfile:1
FROM alpine
ENV API_TOKEN=example-valuedocker build -t env-leak .
docker inspect env-leak --format '{{.Config.Env}}'Setting ENV API_TOKEN=$API_TOKEN from an ARG does the same thing. The secret is now part of the image's permanent configuration.
Leak path 3: COPY
COPY stores file contents in a layer. Copying .npmrc, .env, a private key, or a cloud credentials file into the image puts it there for good, even if you delete it afterward.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
COPY .pypirc /root/.pypirc
RUN pip install -r requirements.txt
RUN rm /root/.pypircThe RUN rm creates a layer that hides the file. The COPY layer still contains it. Another common version is COPY . . with no .dockerignore. That copies any credential file in the build context.
Summary of the leaks
| Instruction | Where the secret is stored | How to see it |
|---|---|---|
| `ARG` | Image history, in the expanded command | `docker history --no-trunc` |
| `ENV` | Image configuration | `docker inspect` |
| `COPY` | A layer's filesystem | Extract layers, or run the image and read the file from an earlier layer |
Common mistakes
- Believing that
rmin a laterRUNremoves data from the image. It does not. - Treating
ARGas private because it is not available at run time. It is still in the history. - Using a multi-stage build and assuming the first stage is safe. Layers from the final stage are what ship, so a secret in the final stage leaks. A secret only in an earlier stage does not ship, but the build cache on the build host can still hold it.
- Forgetting
.dockerignorefor files such as.envand.npmrc.
The common cause is that these instructions write data into the image. A secret mount avoids this because the data exists only while one RUN step executes, and it is never committed to a layer. The next chapter shows how to use it.
Exercise
A Dockerfile has ARG TOKEN, then RUN curl -H "Authorization: $TOKEN" https://example.com/pkg -o pkg, and no later cleanup. Where can someone find the token value in the finished image, and would adding RUN unset TOKEN fix it?
Show answer
The expanded command is stored in the image history, so docker history --no-trunc shows it. RUN unset TOKEN does not fix it, because it only affects the shell of that later step. The earlier history entry is already recorded. The fix is to pass the token through a secret mount instead of an ARG.
Exercise
Why does RUN rm .npmrc after COPY .npmrc . not keep the file out of the image?
Show answer
COPY commits the file into its own layer. rm adds a later layer that marks the file as deleted, but the earlier layer still contains the file and can be extracted from the image.