0
0
MLOpsdevops~5 mins

Multi-stage builds for smaller images in MLOps - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Multi-stage builds for smaller images
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

Look at the build steps that repeat or grow with input size.

  • Primary operation: The RUN go build command compiles the source code.
  • How many times: This compile step runs once per build, processing all source files.
How Execution Grows With Input

As the source code size grows, the compile step takes longer.

Input Size (source files)Approx. Operations (compile time)
10Short compile time
100Longer compile time
1000Much longer compile time

Pattern observation: Compile time grows roughly with the amount of source code.

Final Time Complexity

Time Complexity: O(n)

This means the build time grows linearly with the size of the source code.

Common Mistake

[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.

Interview Connect

Understanding how build steps scale helps you explain trade-offs in build speed and image size clearly.

Self-Check

What if we added caching for the build stage? How would the time complexity change?