Multi-stage builds for smaller images in MLOps - Time & Space Complexity
We want to understand how the time to build a Docker image changes when using multi-stage builds.
How does adding stages affect the build steps as the project grows?
Analyze the time complexity of this multi-stage Dockerfile snippet.
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]
This builds the app in one stage, then copies only the final binary to a smaller image in the second stage.
Look at the build steps that repeat or grow with input size.
- Primary operation: The
RUN go buildcommand compiles the source code. - How many times: This compile step runs once per build, processing all source files.
As the source code size grows, the compile step takes longer.
| Input Size (source files) | Approx. Operations (compile time) |
|---|---|
| 10 | Short compile time |
| 100 | Longer compile time |
| 1000 | Much longer compile time |
Pattern observation: Compile time grows roughly with the amount of source code.
Time Complexity: O(n)
This means the build time grows linearly with the size of the source code.
[X] Wrong: "Multi-stage builds always make the build faster."
[OK] Correct: Multi-stage builds reduce final image size but the compile step still processes all source code once, so build time depends on code size.
Understanding how build steps scale helps you explain trade-offs in build speed and image size clearly.
What if we added caching for the build stage? How would the time complexity change?