Layer caching and ordering in Docker - Time & Space Complexity
When building Docker images, the order of commands affects how long builds take. We want to understand how build time grows as we add more layers.
How does changing the order of steps impact the time to rebuild an image?
Analyze the time complexity of this Dockerfile snippet.
FROM python:3.12-slim
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
COPY src/ /app/src/
RUN python /app/src/setup.py
This Dockerfile installs dependencies first, then copies source code and runs setup.
Look for repeated work when rebuilding the image.
- Primary operation: Running
pip installand copying files. - How many times: Each build may rerun
pip installif requirements change, or copy source files if they change.
As the number of dependencies or source files grows, build time changes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 dependencies | Fast install, quick copy |
| 100 dependencies | Longer install, same copy time |
| 1000 dependencies | Much longer install, same copy time |
Changing source files triggers copying and setup steps, which grow with source size. Installing dependencies grows with number of packages.
Time Complexity: O(n)
This means build time grows roughly in direct proportion to the number of dependencies or source files changed.
[X] Wrong: "Changing source files always triggers reinstalling dependencies."
[OK] Correct: Docker caches layers, so if dependency files don't change, install step is skipped, saving time.
Understanding layer caching helps you write efficient Dockerfiles and speeds up builds, a useful skill in real projects and interviews.
What if we moved the COPY src/ /app/src/ step before installing dependencies? How would the time complexity change?
Practice
Solution
Step 1: Understand Docker layer caching
Docker caches each build step as a layer. If a step and all previous steps are unchanged, Docker reuses the cached layer.Step 2: Effect of instruction order on caching
Placing stable instructions early means those layers rarely change and get reused, speeding up rebuilds.Final Answer:
It speeds up rebuilds by reusing cached layers -> Option DQuick Check:
Layer caching = faster rebuilds [OK]
- Thinking order reduces image size automatically
- Believing order affects container runtime speed
- Confusing readability with caching benefits
Solution
Step 1: Identify stable vs frequently changing instructions
ENV instructions usually change less often than copying app code or installing packages.Step 2: Place stable instructions early
Placing ENV early helps cache it and subsequent layers, avoiding rebuilds when code changes.Final Answer:
ENV APP_ENV=production -> Option BQuick Check:
Stable instructions early = better cache [OK]
- Putting COPY before ENV causing cache bust
- Installing packages after code copy
- Assuming RUN commands never change
app.py and rebuild?FROM python:3.12 COPY requirements.txt /app/ RUN pip install -r /app/requirements.txt COPY app.py /app/ CMD ["python", "/app/app.py"]
Solution
Step 1: Identify which layers change
Changing app.py affects only the COPY app.py step and later steps.Step 2: Understand Docker caching behavior
Layers before COPY app.py remain unchanged and cached, so only last COPY and CMD rebuild.Final Answer:
Only the last COPY and CMD steps are rebuilt; earlier layers are cached -> Option AQuick Check:
Change in later file = rebuild later layers only [OK]
- Assuming all layers rebuild on any file change
- Thinking RUN step rebuilds when app.py changes
- Believing CMD step never rebuilds
FROM node:20 COPY . /app RUN npm install CMD ["node", "/app/index.js"]
After changing
index.js, the npm install step runs again. Why?Solution
Step 1: Analyze Dockerfile order and cache invalidation
COPY . /app copies all files including index.js before npm install step.Step 2: Effect of changing index.js on cache
Changing index.js changes the context for COPY . /app, invalidating cache for that step and all following steps, including npm install.Final Answer:
Because COPY . /app invalidates cache for all later steps -> Option AQuick Check:
Later COPY changes bust cache for previous RUN [OK]
- Thinking npm install always runs
- Assuming index.js change affects package.json
- Believing COPY . /app is placed after npm install
FROM python:3.12 COPY . /app RUN pip install -r /app/requirements.txt CMD ["python", "/app/app.py"]
What is the best change to improve layer caching?
Solution
Step 1: Identify frequent changes and stable files
Source code changes often, requirements.txt changes less frequently.Step 2: Reorder Dockerfile for caching
Copy requirements.txt and run pip install first to cache dependencies, then copy source code to avoid reinstalling on code changes.Final Answer:
Copy only requirements.txt first, run pip install, then copy rest of source code -> Option CQuick Check:
Separate dependencies copy for caching [OK]
- Moving CMD before RUN (invalid order)
- Combining COPY and RUN loses cache benefits
- Adding ENV doesn't affect caching of installs
Ready to prove it?
Pass the test and this pattern moves unknown
