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 /app
COPY --from=builder /app/dist ./dist
FROM golang:alpine AS build
...
FROM scratch
COPY --from=build /out/app /app
COPY --from=build /workspace/target/app.jar app.jar
Next.js: deps (npm ci) → builder (next build) → runner (copy .next/standalone, .next/static and public, then node server.js).

Related terms

Related guides

Taught in

References

Certification courses