Understanding Docker Multi-stage Builds:
- Building an image requires a base image; all subsequent operations are based on this base image.
- Docker image files have a layered structure. Each
RUNinstruction adds a new layer, so reducing the number of layers helps minimize image size. - When multiple
FROMinstructions are used, only the lastFROMimage becomes the root image of the final build.
Example of multi-stage build in my own project deployment: Here, we compile a binary using the Golang base image and then directly copy it into a minimal Alpine-based image: