# Multi Stage Dockerfile

> Use when: create or review optimized multi-stage Dockerfiles for smaller, safer, reproducible application images.

- Skill: `kimtth/multi-stage-dockerfile` (Agent Skill)
- Install (CLI): `npx skillmds@latest add kimtth/multi-stage-dockerfile`
- Raw SKILL.md: https://api.skillmd.com/api/skills/kimtth/multi-stage-dockerfile/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: kimtth (https://skillmd.com/u/kimtth)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/kimtth/multi-stage-dockerfile

---


Goal: produce a Dockerfile with separate build and runtime concerns.

Use for:
- new Dockerfiles
- reducing image size
- removing build tools or secrets from runtime images
- improving layer cache and reproducibility

Workflow:
1. Detect language, package manager, build command, test command, and runtime port.
2. Use a builder stage for dependencies, compilation, and tests.
3. Use a runtime stage with only required artifacts and runtime deps.
4. Pin base image versions and keep layers cache-friendly.
5. Add `.dockerignore`, non-root user, and healthcheck when appropriate.
6. Build and smoke test the image.

Best practices:
- name stages with `AS builder`, `AS runtime`, etc.
- copy lockfiles before source for dependency caching
- avoid secrets in `ARG`, `ENV`, layers, or final image
- use minimal official or distroless images when compatible

Rules:
- do not use `latest` tags for reproducible builds
- do not run as root unless required
- do not copy the whole build context blindly
- verify the runtime image starts

