toolhq.io

skills / devops and infrastructure

review-a-dockerfile

The three things that actually bite in a container build: a defeated layer cache, a toolchain shipped to production, and PID 1 dropping SIGTERM.

SKILL.md

Save as .claude/skills/review-a-dockerfile/SKILL.md in a repository, or under ~/.claude/skills/review-a-dockerfile/SKILL.md to have it everywhere. The copy button takes the whole file, frontmatter included.

---
name: review-a-dockerfile
description: Review a Dockerfile or container image for correctness, build speed, size and security, ranked by what actually bites. Use when asked to review a Dockerfile, shrink an image, speed up a container build, or harden a container before it runs in production.
---

# Review a Dockerfile

Most Dockerfile reviews produce a list of micro-optimisations and miss the three
things that matter: the build is slow because the layer cache is defeated, the
image is large because the build toolchain shipped with it, and it runs as root.

Read the whole file and work out what the image is for before commenting. An
image for local development and an image serving production traffic deserve
different reviews.

## 1. Cache ordering, the biggest win

Docker caches per instruction and invalidates everything after the first
changed layer. Almost every slow build is this.

The rule: **instructions ordered least to most frequently changing.**

The specific mistake, which is near universal:

```dockerfile
COPY . .
RUN npm ci
```

Any change to any file invalidates the `COPY`, so dependencies reinstall on
every build. Copy the manifest first:

```dockerfile
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
```

Now dependencies reinstall only when the lockfile changes. The same shape
applies to `requirements.txt`, `go.mod`, `Gemfile.lock`, `pom.xml`, `Cargo.toml`.

Related checks:

- Is there a `.dockerignore`. Without one, `COPY . .` sends `.git`,
  `node_modules`, local env files and build output into the context. This is
  slow, it busts the cache constantly, and it is how secrets end up in images.
- `apt-get update` and `apt-get install` must be in the same `RUN`. Split
  across two, a cached `update` layer feeds stale package indexes to a fresh
  `install`.
- An unpinned base tag such as `node:22` changes under you. Pin to a digest for
  reproducibility, or at least a full patch version.

## 2. Size, which is mostly multi-stage

If the final image contains a compiler, a package manager's cache, or the
source code of dependencies that were compiled, it is larger than it needs to
be and has a wider attack surface than it needs.

```dockerfile
FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/server

FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
```

Other size checks:

- Clean up in the same `RUN` that creates the files. A `rm -rf /var/lib/apt/lists/*`
  in a later layer removes nothing from the image, it only hides it. Deleted
  files still occupy the earlier layer.
- `--no-install-recommends` for apt, `--no-cache-dir` for pip,
  `npm ci --omit=dev` for production node images.
- Prefer slim or distroless bases where the runtime allows. Alpine is smaller
  but uses musl, which changes DNS resolution behaviour and breaks some
  prebuilt wheels and native modules. That is a tradeoff to make deliberately,
  not a default.

## 3. Security

- **Running as root.** The default, and a finding every time for anything
  serving traffic. Create a user and `USER` to it before the entrypoint.
  Combined with a container escape, root in the container is a materially worse
  day.
- **Secrets in build arguments.** `ARG` values are visible in image history.
  Use BuildKit secret mounts (`RUN --mount=type=secret,...`) for anything that
  must not persist. A secret in an `ENV` is in the image permanently.
- A `COPY . .` with no `.dockerignore` that pulls in `.env` or `.git`. Check
  with `docker history` and by listing the image filesystem, not by reading the
  Dockerfile.
- `curl | sh` installs in a build. No integrity check, no pinned version, and
  the result changes silently.
- `ADD` with a URL, which fetches without verification. Use `COPY` for local
  files; use `RUN curl` with a checksum for remote ones.
- Unnecessary `EXPOSE` of ports the process does not listen on, which is
  misleading rather than dangerous.

## 4. Runtime correctness

These produce production incidents rather than review comments, so check them.

- **Signal handling.** `CMD npm start` runs through a shell as PID 1, which
  does not forward `SIGTERM`. The container is then killed rather than shut
  down gracefully, dropping in-flight requests on every deploy. Use exec form
  (`CMD ["node", "server.js"]`), and an init such as `tini` where the process
  spawns children.
- Exec form versus shell form generally. `CMD node server.js` and
  `CMD ["node", "server.js"]` behave differently and the difference only shows
  under termination.
- A `HEALTHCHECK` that tests the process rather than the dependency it needs is
  worse than none, because it reports healthy during an outage.
- `WORKDIR` rather than `RUN cd`, which does not persist between instructions.
- Does the image assume a writable filesystem. If it runs read-only in
  production, that needs to be tested in the build, not discovered at deploy.
- Platform. An image built on an arm64 laptop and deployed to amd64 fails at
  runtime unless built with `--platform` or buildx.

## Reporting

Rank by consequence:

1. Secrets in the image, running as root, signal handling that breaks deploys.
2. Cache ordering, because it costs the team time on every single build.
3. Size.
4. Style.

Give the replacement lines, not a description of them. For size and build-time
claims, say whether you measured or are estimating, and prefer measuring:
`docker build` timings and `docker image ls` settle it in a minute.

what it needs

Nothing. This skill is instructions only, so it works in any agent that reads SKILL.md with nothing installed and no account. Save the file and it is live in the next session.

the instructions, in full

Review a Dockerfile

Most Dockerfile reviews produce a list of micro-optimisations and miss the three things that matter: the build is slow because the layer cache is defeated, the image is large because the build toolchain shipped with it, and it runs as root.

Read the whole file and work out what the image is for before commenting. An image for local development and an image serving production traffic deserve different reviews.

1. Cache ordering, the biggest win

Docker caches per instruction and invalidates everything after the first changed layer. Almost every slow build is this.

The rule: instructions ordered least to most frequently changing.

The specific mistake, which is near universal:

COPY . .
RUN npm ci

Any change to any file invalidates the COPY, so dependencies reinstall on every build. Copy the manifest first:

COPY package.json package-lock.json ./
RUN npm ci
COPY . .

Now dependencies reinstall only when the lockfile changes. The same shape applies to requirements.txt, go.mod, Gemfile.lock, pom.xml, Cargo.toml.

Related checks:

2. Size, which is mostly multi-stage

If the final image contains a compiler, a package manager's cache, or the source code of dependencies that were compiled, it is larger than it needs to be and has a wider attack surface than it needs.

FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/server

FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]

Other size checks:

3. Security

4. Runtime correctness

These produce production incidents rather than review comments, so check them.

Reporting

Rank by consequence:

  1. Secrets in the image, running as root, signal handling that breaks deploys.
  2. Cache ordering, because it costs the team time on every single build.
  3. Size.
  4. Style.

Give the replacement lines, not a description of them. For size and build-time claims, say whether you measured or are estimating, and prefer measuring: docker build timings and docker image ls settle it in a minute.