multi-stage build
A Dockerfile with several FROM stages, where later stages copy only what they need from earlier ones.
In more detail
Build tools, compilers and test dependencies live in an early stage; the final stage starts from a small base and copies in just the finished application with COPY --from=build. The shipped image is smaller and carries fewer vulnerabilities, and each stage can be built on its own as a build target.
Examples
FROM python:3.12 AS build
...
FROM python:3.12-slim
COPY --from=build /app /appCOPY --from=builder /app/dist ./distFROM golang:alpine AS build
...
FROM scratch
COPY --from=build /out/app /appCOPY --from=build /workspace/target/app.jar app.jarNext.js: deps (npm ci) → builder (next build) → runner (copy .next/standalone, .next/static and public, then node server.js).Related terms
- build target — The named stage of a multi-stage Dockerfile you ask docker build to stop at.
- FROM — The Dockerfile instruction that picks the base image a build starts from.
- minimal base image — A base image with as little inside as possible, such as Alpine, a slim variant or a distroless image, to shrink size and attack surface.
- COPY — The Dockerfile instruction that copies files from the build context into the image.
- Dockerfile — A text file of instructions, such as FROM, COPY and RUN, that tells Docker how to build an image.
- attack surface — Everything an attacker could try to use against a system: open ports, installed programs, accounts and settings.
- JDK — The Java Development Kit: the Java runtime plus the tools that build programs, such as the compiler javac, the disassembler javap and the jar tool.
- JRE — The JVM plus the Java standard library: everything needed to run a compiled Java program, but no compiler.
- base image — The image a Dockerfile starts from with FROM, which supplies the operating system files and often a language runtime.
- standalone output — A next.config setting that makes next build produce a self-contained folder with a minimal server.js and only the node_modules files it needs.
Related guides
Taught in
- Container Images The Exam Way — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Design And Build: Timed Drill And Checkpoint — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Container Hardening: Distroless & Rootless — Cloud Security & DevSecOps Engineering
- CMD, ENTRYPOINT, USER And Process Signals — Docker Architecture & Production Containers
- Multi Stage Builds, Targets And Build Secrets — Docker Architecture & Production Containers
- Minimal Bases, Non Root Users And Smaller Images — Docker Architecture & Production Containers
- Containerization Architecture: Multi-Stage Dockerfiles — Production Full-Stack Engineering
- Shipping in Scratch and Distroless Images — Concurrent Systems Programming with Go
- The Virtual Machine, The Kit And The Runtime — Modern Java: Virtual Threads & High-Throughput Services
- Packaging Into A Container — Modern Java: Virtual Threads & High-Throughput Services
- Instrumentation: OpenTelemetry & Performance Spans — Next.js 15 & Modern App Router Architecture
- Containerized Deployment: Multi-Stage Docker Builds — Next.js 15 & Modern App Router Architecture
- Production Hardening: Graceful Shutdown & Docker — Node.js Internals & Backend Services
- Container Security: Minimal Bases And Non Root — Application Security & Threat Modeling for Engineers