Image size and minimal base images in Docker - Time & Space Complexity
We want to understand how the size of a Docker image grows when we choose different base images.
How does the choice of base image affect the total image size as we add more layers?
Analyze the time complexity of the following Dockerfile snippet.
FROM alpine:latest
RUN apk add --no-cache curl
COPY app /app
RUN chmod +x /app/start.sh
CMD ["/app/start.sh"]
This Dockerfile uses a minimal base image and adds a few layers for app setup.
Look for repeated steps that add to image size.
- Primary operation: Each Dockerfile instruction creates a new layer.
- How many times: Four instructions add layers here (FROM, RUN, COPY, RUN).
As we add more instructions, the image size grows roughly by the size of each added layer.
| Input Size (number of layers) | Approx. Image Size |
|---|---|
| 2 | Small (minimal base + 1 layer) |
| 5 | Moderate (minimal base + 4 layers) |
| 10 | Larger (minimal base + 9 layers) |
Pattern observation: Image size grows roughly linearly with the number of layers added.
Time Complexity: O(n)
This means the total image size grows in direct proportion to the number of layers added.
[X] Wrong: "Using a minimal base image always makes the image size constant regardless of added layers."
[OK] Correct: Even with a small base, each added layer increases the image size, so size grows with added instructions.
Understanding how image size grows helps you build efficient containers and shows you think about resource use clearly.
"What if we combined multiple RUN commands into one? How would that affect the image size growth?"
Practice
Solution
Step 1: Understand common base images
Ubuntu, Debian, and CentOS are full Linux distributions and tend to be larger in size.Step 2: Identify minimal base images
Alpine is a lightweight Linux distribution designed for minimal size, often under 10 MB.Final Answer:
Alpine -> Option BQuick Check:
Minimal base image = Alpine [OK]
- Choosing Ubuntu or Debian as minimal base images
- Confusing full distributions with minimal ones
- Not knowing Alpine is designed for small size
Solution
Step 1: Recall Dockerfile syntax for base images
The correct keyword to specify a base image is FROM followed by the image name and optional tag.Step 2: Check each option
Only 'FROM alpine:latest' uses the correct syntax. Others are invalid Dockerfile commands.Final Answer:
FROM alpine:latest -> Option AQuick Check:
Base image syntax = FROM [OK]
- Using incorrect keywords like BASE or USE
- Omitting the FROM keyword
- Not specifying image tag correctly
FROM alpine:3.18 RUN apk add --no-cache curlWhat is the main reason this image remains small compared to using Ubuntu as base?
Solution
Step 1: Understand Alpine's package manager behavior
Alpine uses apk, and the --no-cache option prevents storing temporary cache files, keeping the image small.Step 2: Compare with Ubuntu
Ubuntu uses apt which caches package files unless explicitly cleaned, increasing image size.Final Answer:
Alpine uses apk with --no-cache to avoid cache files, reducing size -> Option DQuick Check:
apk --no-cache reduces image size [OK]
- Thinking Alpine installs packages faster
- Believing Ubuntu can't install curl
- Assuming Alpine removes all packages automatically
FROM alpine RUN apk add curlBut your final image is much larger than expected. What is the likely cause?
Solution
Step 1: Analyze apk add command
Without --no-cache, apk stores cache files in the image, increasing size.Step 2: Understand base image size and syntax
Alpine is small by default, RUN syntax is correct, and Ubuntu is larger, so those are not causes.Final Answer:
You forgot to use --no-cache with apk add, so cache files remain -> Option AQuick Check:
Missing --no-cache causes larger images [OK]
- Assuming Alpine is large by default
- Using wrong RUN syntax
- Switching to Ubuntu thinking it is smaller
Solution
Step 1: Understand the scratch base image
Scratch is an empty image with zero size, ideal for copying only necessary files like a compiled binary.Step 2: Compare other options
Ubuntu, Alpine, and Debian add extra layers and packages, increasing image size unnecessarily.Final Answer:
Use FROM scratch and copy only the compiled Go binary -> Option CQuick Check:
Scratch + compiled binary = smallest image [OK]
- Installing full runtimes increasing image size
- Using larger base images unnecessarily
- Not using multi-stage builds to compile separately
Ready to prove it?
Pass the test and this pattern moves unknown
