Why use a multi-stage Docker build?
Options
- To run several containers at once from a single Dockerfile
- To compile in one stage and copy only the artifacts into a small image (Correct option)
- To build for several CPU architectures automatically in one step
- To avoid writing a Dockerfile by reusing the one from another image
Answer
To build/compile in one stage and copy only the artifacts into a small runtime image
Why
Building often needs heavy tools (compilers, build dependencies, Node to bundle a frontend) that the running app does not need.
A multi-stage build uses a first 'builder' stage with all tools, then a second, slim stage that copies only the finished result.
The final image is smaller, downloads faster and has fewer things an attacker could exploit.
Remember
Multi-stage = build big, ship small.