Reducing final image size by 80 percent in Docker - Time & Space Complexity
When we reduce a Docker image size, we want to know how the effort grows as the image content grows.
We ask: How does the time to build and optimize the image change as the image layers or files increase?
Analyze the time complexity of the following Dockerfile snippet.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN rm -rf tests docs
CMD ["python", "app.py"]
This Dockerfile installs dependencies, copies app files, and removes unnecessary folders to reduce image size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying files and installing dependencies.
- How many times: Each file and dependency is processed once during build.
As the number of files and dependencies grows, the build time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files/deps | About 10 operations |
| 100 files/deps | About 100 operations |
| 1000 files/deps | About 1000 operations |
Pattern observation: Doubling files or dependencies roughly doubles the work.
Time Complexity: O(n)
This means the build time grows linearly with the number of files and dependencies processed.
[X] Wrong: "Removing files after copying them doesn't affect build time much."
[OK] Correct: Removing files still requires processing those files during copy, so the build time depends on total files handled.
Understanding how build time scales with image content helps you explain trade-offs in Docker optimization clearly and confidently.
"What if we used multi-stage builds to copy only needed files? How would the time complexity change?"
Practice
Solution
Step 1: Understand multi-stage builds purpose
Multi-stage builds let you use one stage to build your app and another to create a smaller final image.Step 2: Identify how size is reduced
By copying only the needed files from the build stage to the final stage, unnecessary build tools and files are excluded.Final Answer:
It allows copying only necessary files to the final image, reducing size. -> Option AQuick Check:
Multi-stage builds copy only needed files [OK]
- Thinking multi-stage speeds up build time
- Assuming multi-stage compresses layers automatically
- Confusing multi-stage with running multiple containers
Solution
Step 1: Identify multi-stage build syntax
Multi-stage builds use 'AS' to name stages and 'COPY --from=stage' to copy files.Step 2: Check base images for build and final stages
FROM node:18 AS build RUN npm install FROM node:18-slim COPY --from=build /app /app uses full node image for build and smaller node:18-slim for final, which reduces size.Final Answer:
FROM node:18 AS build RUN npm install FROM node:18-slim COPY --from=build /app /app -> Option DQuick Check:
Correct multi-stage syntax with smaller final image [OK]
- Not using 'AS' to name build stage
- Using larger base image in final stage
- Copying files without specifying source stage
FROM golang:1.20 AS build WORKDIR /app COPY . . RUN go build -o myapp FROM alpine:latest COPY --from=build /app/myapp /myapp CMD ["/myapp"]
Solution
Step 1: Analyze base images used
The build stage uses golang:1.20 (large image with build tools), final uses alpine (very small).Step 2: Understand what is copied to final image
Only the compiled binary is copied, excluding all build dependencies, drastically reducing size.Final Answer:
Final image is about 80% smaller than the build image. -> Option AQuick Check:
Copying only binary to alpine reduces size ~80% [OK]
- Assuming final image includes build tools
- Thinking final image is bigger due to alpine
- Believing size difference is minimal
FROM python:3.12 AS build WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . FROM python:3.12 COPY --from=build /app /app CMD ["python", "/app/app.py"]What is the main issue causing the large final image?
Solution
Step 1: Check base images in build and final stages
Both stages use python:3.12, which is a large image; no size reduction from multi-stage.Step 2: Identify how to reduce size
Using a smaller base like python:3.12-slim or alpine in the final stage reduces image size significantly.Final Answer:
The final stage uses the full python:3.12 image instead of a smaller base. -> Option BQuick Check:
Final stage base image too large [OK]
- Thinking pip install must run in final stage
- Assuming COPY syntax is wrong
- Believing requirements.txt missing causes large image
Solution
Step 1: Understand build and runtime needs
Building requires full Node.js image with tools; running only needs compiled files and a small base.Step 2: Choose multi-stage build with appropriate images
First stage uses full node image to build; second stage uses smaller node:alpine image copying only build output to reduce size.Final Answer:
Use multi-stage build: first stage with full node image to build, second stage with node:alpine copying only build output. -> Option CQuick Check:
Multi-stage with full build image and small runtime image [OK]
- Using single stage and deleting files later
- Building on small image lacking tools
- Running on large image unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
