Docker Course
Docker
/
Advanced

Multi-Stage Builds

Definition

A method of defining multiple `FROM` statements in a single Dockerfile. It allows you to use one environment to compile the code (with all the heavy build tools), and a completely different, pristine environment to run the final code, discarding the heavy tools.

Explain Like I'm New

Using a massive, messy kitchen to bake the cake, but then moving ONLY the finished cake into a pristine, tiny glass display case to sell it. You don't sell the dirty pots and pans.

Real World Example

A Golang app requires 1GB of compilers to build. In Stage 1 (`FROM golang`), you compile the code into a 10MB binary file. In Stage 2 (`FROM alpine`), you copy *only* the 10MB binary from Stage 1. The final image size is 15MB instead of 1GB.

Common Use Cases

  • •Massively reducing image size
  • •Removing vulnerabilities

Interview Questions

basic

  • In a Multi-Stage build with three `FROM` statements, how many final Images are produced?

intermediate

  • What is the primary security benefit of using a Multi-Stage Build?

Flash Cards

Question

How many final images?

Click to reveal answer
Answer

Exactly one. Only the final stage is saved as the actual Image. The previous stages are used as temporary 'builder' environments and then discarded.

Question

Security benefit?

Click to reveal answer
Answer

It significantly reduces the 'Attack Surface'. By discarding the compiler, debuggers, and source code in Stage 1, hackers have far fewer tools available to exploit if they manage to breach the final Stage 2 container.