Sub topic 10
Keeping secrets and .git out of images
This chapter assumes you can write a Dockerfile with FROM, COPY and RUN, and that you know COPY . . copies the build context into the image. Everything in the context can end up in an image layer, and anyone who can pull the image can read it.
How secrets get into images
The usual cause is a broad copy instruction:
FROM node:20-slim
WORKDIR /app
COPY . .
RUN npm ci
CMD ["node", "server.js"]If the context root holds a .env file, a private key, or the .git directory, COPY . . copies them into the image. The .git directory is a problem because it holds the full history. A password removed from the code two years ago is still in an old commit.
Deleting the file in a later step does not fix this:
COPY . .
RUN rm .envEach instruction creates a layer, and the layer made by COPY still contains .env. Tools such as docker save can extract it. The only reliable fix is to keep the file out of the context.
A .dockerignore for secrets
Put this file at the context root:
# Version control history
.git
.gitignore
# Environment files and credentials
.env
.env.*
!.env.example
**/*.pem
**/*.key
.aws
.npmrcThree details matter here:
.env.*does not match.env, so both lines are needed.*.pemwithout**/only matches files in the context root.**/*.pemmatches at any depth.!.env.examplecomes after.env.*because the last matching rule wins. It keeps a harmless template file.
Verify the result
Build the image, then list the files inside it:
docker build -t myapp .
docker run --rm myapp ls -a /appIf .env or .git appears in the output, a pattern is wrong or the .dockerignore file is not at the context root. The debug image from the previous chapter, which copies the whole context and runs find, gives the same answer without involving your application image.
When the build itself needs a secret
Sometimes a build step needs a credential, such as a private npm registry token. Do not copy the file in and do not pass it as a --build-arg, because build arguments appear in docker history. Use a BuildKit secret mount instead:
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
COPY . .
CMD ["node", "server.js"]docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp .The secret is mounted only while that RUN step executes. It is not part of the context, and it is not written to any layer. Keep .npmrc in .dockerignore as well, so COPY . . cannot pick up a project-level copy.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `RUN rm .env` after `COPY` | File remains in the earlier layer | Exclude it with `.dockerignore` |
| `ARG TOKEN` used in `RUN` | Value visible in `docker history` | Use `--mount=type=secret` |
| `*.pem` for nested keys | Only root-level keys excluded | Use `**/*.pem` |
| Ignore file in the wrong directory | No filtering happens | Place it at the context root |
Exercise
A project has .env in its root and config/prod.key in a subdirectory. Which two .dockerignore lines keep both out of the build context, and why does *.key alone fail?
Show answer
Use .env and **/*.key. A pattern like *.key matches only files in the context root, so config/prod.key would still be sent to the builder. The **/ prefix matches at any directory depth.
Exercise
You added RUN rm .env after COPY . .. Does the image still contain the secret? How would you remove it properly?
Show answer
Yes. The layer created by COPY . . still contains .env, and the later rm only hides it in the final filesystem. Add .env to .dockerignore and rebuild so the file never enters the context.