FROM instruction for base image in Docker - Time & Space Complexity
We want to understand how the time it takes to build a Docker image grows when using the FROM instruction.
Specifically, how does choosing a base image affect the build time as the image size changes?
Analyze the time complexity of this Dockerfile snippet.
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl
COPY app /app
CMD ["/app/start.sh"]
This snippet starts from an Ubuntu base image, installs packages, copies app files, and sets the command to run.
Look for repeated steps that affect build time.
- Primary operation: Downloading and extracting the base image layers.
- How many times: Once per build if not cached; depends on number of layers in the base image.
The build time grows as the base image size and number of layers increase.
| Input Size (MB) | Approx. Operations (Download & Extract) |
|---|---|
| 50 | Low |
| 200 | Medium |
| 1000 | High |
Pattern observation: Larger base images take longer to download and extract, so build time increases roughly with image size.
Time Complexity: O(n)
This means build time grows linearly with the size of the base image.
[X] Wrong: "The FROM instruction always takes the same time regardless of the base image size."
[OK] Correct: Larger base images have more data to download and extract, so they take more time.
Understanding how base image size affects build time helps you make smart choices in real projects and shows you think about efficiency.
What if we used a multi-stage build with smaller base images? How would the time complexity change?