Multi-stage Builds
Some programming languages (like Go, Rust, or C++) must be compiled before they run — a compiler is a tool that translates the source code a programmer wrote into a single program the machine can run directly (a binary file). In a multi-stage build you use a builder stage with the full toolchain to
You cook in a big messy kitchen with lots of pots and raw ingredients — then you send the customer only the finished plate, without the pots and without the leftovers.
- compiler
- A tool that translates the source code a programmer wrote into a program the machine can run directly (a binary file). Compiled languages like Go, Rust, and C++ need it to build the program — but not to run it.
- build stage
- The first stage in a multi-stage build, based on an image with a full toolchain (compiler, libraries), where the application is compiled into a final artifact.
- runtime stage
- The final stage, based on a small image (for example alpine), containing only the finished artifact and what is needed to run it — without the build tools.
- COPY --from
- An instruction that copies files from an earlier stage (by name or index) into the current stage, letting you pick only the final artifact and leave the rest of the build behind.