Multi-Stage Builds for Lean Production Images
Master multi-stage Dockerfiles to compile or bundle your app in a build stage and copy only the artifacts into a tiny runtime image.
Multi-Stage Builds for Lean Production Images is a free Docker & Kubernetes for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & Kubernetes for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Fat Image Problem
A single-stage build often ships compilers, dev dependencies, and source code into production. The result is a large, slower-to-pull, larger-attack-surface image.
What Is a Multi-Stage Build
A multi-stage build uses several FROM statements in one Dockerfile. Earlier stages build artifacts; a final lean stage copies only what it needs to run.
A Go Multi-Stage Example
Compile in a Go image, then ship a tiny final image with just the binary.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN go build -o app .
FROM alpine:3.20
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]Copying From a Named Stage
The key line is COPY --from=build. It pulls the compiled app binary out of the build stage, leaving the Go toolchain behind.
A Node.js Multi-Stage Example
Install and build with dev deps, then copy only the production output.
FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package*.json ./
RUN npm ci --omit=dev
CMD ["node", "dist/index.js"]Choosing a Slim Base
Final stages should use minimal bases like alpine, -slim, or distroless. Smaller bases mean fewer packages and fewer vulnerabilities.
Distroless Images
Google distroless images contain only your app and its runtime, no shell or package manager. They are very small and hard to exploit interactively.
FROM gcr.io/distroless/static-debian12
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]Targeting a Specific Stage
You can build only up to a named stage with --target, useful for running tests in CI.
docker build --target build -t myapp:test .Caching Dependencies
Copy dependency manifests before the rest of the source. Then dependency installation is cached and only re-runs when the manifest changes.
COPY package*.json ./
RUN npm ci
COPY . .Measuring the Win
Compare sizes before and after. Multi-stage builds commonly shrink images from hundreds of MB to tens of MB.
docker images myappCombining With .dockerignore
Add a .dockerignore so build context excludes node_modules, .git, and secrets, speeding builds and avoiding leaking files into images.
node_modules
.git
*.envQuick Check
Test what you have learned.
Recap
You learned to split Dockerfiles into build and runtime stages, copy only artifacts with --from, pick slim or distroless bases, target stages, and combine with .dockerignore for lean production images.
Frequently asked questions
Is the “Multi-Stage Builds for Lean Production Images” lesson free?
Yes — the full text of “Multi-Stage Builds for Lean Production Images” is free to read here on the web, and the Docker & Kubernetes for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & Kubernetes for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Multi-Stage Builds for Lean Production Images”?
Master multi-stage Dockerfiles to compile or bundle your app in a build stage and copy only the artifacts into a tiny runtime image. You practise Docker & Kubernetes for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Docker & Kubernetes for Developers?
No prior experience is required. Docker & Kubernetes for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multi-Stage Builds for Lean Production Images” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Docker & Kubernetes for Developers lesson?
Yes. Every Docker & Kubernetes for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Containerizing a Web Application
- Optimizing Docker Images
- Security & Production Best Practices
- Multi-Stage Builds for Lean Production Images