Produce production Docker images that are small, reproducible, cache-efficient, and secure.
When to use
"Write/optimize a Dockerfile" or "containerize this service"
"My image is huge / builds slowly"
Reviewing an existing Dockerfile
Principles (apply these)
Multi-stage build. Compile/install in a builder stage; copy only artifacts into a slim final stage.
Minimal final base. Go → gcr.io/distroless/static or scratch; Python → python:*-slim;
Node → node:*-slim or distroless. Never ship the full SDK image.
Non-root. Create and USER a non-root user in the final stage. Distroless :nonroot works for Go.
Cache-friendly layer order. Copy dependency manifests (go.mod/go.sum, requirements.txt,
package.json/lock) and download deps before copying source, so code changes don't bust the dep cache.
.dockerignore. Exclude .git, build output, node_modules, env files, test data — keeps the
build context (and secret-leak risk) small.
Pin versions. Pin base image tags (and digests for prod); never latest.
No secrets in layers. Use build args/secrets mounts, not COPY .env. Secrets in any layer
persist in history even if later removed.
Healthcheck + metadata. Add a HEALTHCHECK and OCI labels where useful.
Reproducibility. For Go set CGO_ENABLED=0; strip with -ldflags="-s -w".
Reference — Go multi-stage
FROM golang:1.22-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /app ./cmd/service
FROM gcr.io/distroless/static:nonroot
COPY --from=build /app /app
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app"]
Output
When reviewing, list issues by impact (security → size → build speed), then provide the rewritten
Dockerfile and a matching .dockerignore. State the expected before/after image size if known.
1---2name: dockerfile-optimizer3description: Dockerfile Optimizer4---56# Dockerfile Optimizer78Produce production Docker images that are small, reproducible, cache-efficient, and secure.910## When to use11- "Write/optimize a Dockerfile" or "containerize this service"12- "My image is huge / builds slowly"13- Reviewing an existing Dockerfile1415## Principles (apply these)16171. **Multi-stage build.** Compile/install in a builder stage; copy only artifacts into a slim final stage.182. **Minimal final base.** Go → `gcr.io/distroless/static` or `scratch`; Python → `python:*-slim`;19 Node → `node:*-slim` or distroless. Never ship the full SDK image.203. **Non-root.** Create and `USER` a non-root user in the final stage. Distroless `:nonroot` works for Go.214. **Cache-friendly layer order.** Copy dependency manifests (`go.mod/go.sum`, `requirements.txt`,22 `package.json/lock`) and download deps *before* copying source, so code changes don't bust the dep cache.235. **`.dockerignore`.** Exclude `.git`, build output, `node_modules`, env files, test data — keeps the24 build context (and secret-leak risk) small.256. **Pin versions.** Pin base image tags (and digests for prod); never `latest`.267. **No secrets in layers.** Use build args/secrets mounts, not `COPY .env`. Secrets in any layer27 persist in history even if later removed.288. **Healthcheck + metadata.** Add a `HEALTHCHECK` and OCI labels where useful.299. **Reproducibility.** For Go set `CGO_ENABLED=0`; strip with `-ldflags="-s -w"`.3031## Reference — Go multi-stage32```dockerfile33FROM golang:1.22-alpine AS build34WORKDIR /src35COPY go.mod go.sum ./36RUN go mod download37COPY . .38RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /app ./cmd/service3940FROM gcr.io/distroless/static:nonroot41COPY --from=build /app /app42USER nonroot:nonroot43EXPOSE 808044ENTRYPOINT ["/app"]45```4647## Output48When reviewing, list issues by impact (security → size → build speed), then provide the rewritten49Dockerfile and a matching `.dockerignore`. State the expected before/after image size if known.
Run npx skillmds@latest add shravan-amberkar/dockerfile-optimizer in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Dockerfile Optimizer It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Shravan-Amberkar (@shravan-amberkar) published this skill. Their other Agent Skills are listed on their SkillMD profile.