dockerfile-optimizer

Reviews and rewrites Dockerfiles for smaller, faster, safer images - multi-stage builds, layer caching, non-root users and pinned bases. Use when the user asks to optimise, review or write a Dockerfile, or complains about slow builds or large images.

DevOpsSource

Install

mkdir -p ~/.claude/skills/dockerfile-optimizer
# save the file below as ~/.claude/skills/dockerfile-optimizer/SKILL.md

SKILL.md

dockerfile-optimizer/SKILL.md
---
name: dockerfile-optimizer
description: Reviews and rewrites Dockerfiles for smaller, faster, safer images - multi-stage builds, layer caching, non-root users and pinned bases. Use when the user asks to optimise, review or write a Dockerfile, or complains about slow builds or large images.
---

# Dockerfile Optimizer

## Checklist

1. **Base image** – pinned tag (and digest for production); slim/distroless where possible.
2. **Multi-stage** – build tools only in the builder stage; copy just the artifacts.
3. **Cache order** – copy lockfiles and install deps before copying source.
4. **Layers** – combine `apt-get update && apt-get install --no-install-recommends … && rm -rf /var/lib/apt/lists/*` in one `RUN`.
5. **.dockerignore** – exclude `.git`, `node_modules`, env files, build output.
6. **Security** – `USER` non-root; no secrets in `ENV`/`ARG` (use build secrets); `HEALTHCHECK` for services.
7. **Runtime** – exec-form `CMD ["node", "server.js"]` so signals reach the process.

## Node example

```dockerfile
FROM node:22-slim AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile

FROM deps AS build
COPY . .
RUN pnpm build && pnpm prune --prod

FROM node:22-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/.output ./.output
USER node
CMD ["node", ".output/server/index.mjs"]
```

## Output

The rewritten Dockerfile, then a before/after list of what changed and the expected effect (size, build time, security).