Cache mount for faster builds in Docker - Time & Space Complexity
We want to understand how using cache mounts affects the time it takes to build Docker images.
Specifically, how the build time changes as the project size grows when cache is used.
Analyze the time complexity of the following Dockerfile snippet using cache mount.
FROM node:18
RUN --mount=type=cache,target=/root/.npm \
npm install
COPY . .
RUN npm run build
This snippet uses a cache mount to speed up the npm install step by reusing downloaded packages.
Look for repeated work that the cache helps avoid.
- Primary operation: npm install downloads and installs packages.
- How many times: Without cache, this runs fully every build; with cache, only new or changed packages are processed.
As the number of packages grows, install time grows too, but cache reduces repeated work.
| Input Size (number of packages) | Approx. Operations |
|---|---|
| 10 | About 10 installs without cache, fewer with cache |
| 100 | About 100 installs without cache, fewer with cache |
| 1000 | About 1000 installs without cache, fewer with cache |
Pattern observation: Without cache, install time grows roughly linearly with package count; with cache, repeated installs are avoided, so growth slows down.
Time Complexity: O(n)
This means the install step grows roughly in direct proportion to the number of packages, but cache reduces repeated work to save time.
[X] Wrong: "Cache mount makes the build time constant no matter how many packages there are."
[OK] Correct: Cache only skips work for unchanged packages; new or updated packages still take time, so build time still grows with input size.
Understanding how caching affects build time shows you can reason about efficiency in real projects, a useful skill for any DevOps role.
"What if we removed the cache mount and used a fresh install every time? How would the time complexity change?"
Practice
--mount=type=cache in a Dockerfile during build?Solution
Step 1: Understand cache mount purpose
The--mount=type=cacheoption is used during build to save files temporarily between build steps.Step 2: Identify effect on build speed
By saving files like package downloads, it avoids repeating work, making future builds faster.Final Answer:
To save files between build steps and speed up future builds -> Option AQuick Check:
Cache mount speeds builds = D [OK]
- Confusing cache mount with runtime volume mount
- Thinking cache mount stores files in final image
- Assuming cache mount shares files between containers
Solution
Step 1: Check correct mount syntax
The correct syntax uses--mount=type=cache,target=...inside the RUN command.Step 2: Validate options
RUN --mount=type=cache,target=/root/.cache npm install matches the correct syntax exactly; others have wrong keywords or types.Final Answer:
RUN --mount=type=cache,target=/root/.cache npm install -> Option DQuick Check:
Correct cache mount syntax = A [OK]
- Using --cache instead of --mount
- Omitting type=cache
- Using type=volume instead of cache
RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt
What is the expected effect on build time when running the second RUN command?
Solution
Step 1: Understand cache mount effect on repeated commands
The cache mount stores files from the first pip install in /root/.cache.Step 2: Predict second RUN command behavior
The second RUN reuses cached files, so it runs faster by skipping downloads.Final Answer:
The second RUN command will be faster because it reuses cached files -> Option BQuick Check:
Cache reuse speeds second build step = B [OK]
- Assuming cache clears between RUN commands
- Thinking cache causes build failure
- Believing cache is ignored during build
RUN --mount=type=cache,target=/root/.cache npm install
But the cache is not speeding up builds. What is the likely mistake?
Solution
Step 1: Check cache mount target relevance
Cache mount must target the directory where npm stores cache files, usually/root/.npm, not/root/.cache.Step 2: Understand npm cache usage
If the target directory is wrong, npm won't use the cache, so builds stay slow.Final Answer:
The cache mount target directory is incorrect or not used by npm -> Option AQuick Check:
Correct cache target directory needed = C [OK]
- Using wrong directory for cache mount target
- Confusing --cache and --mount flags
- Thinking cache mounts only work for some languages
apt-get update and apt-get install. How can you use cache mounts to optimize this build?Solution
Step 1: Identify apt cache directory
The apt package manager stores downloaded package files in/var/cache/apt.Step 2: Apply cache mount before update
Mounting cache at/var/cache/aptbeforeapt-get updatesaves downloaded package lists and speeds up future runs.Final Answer:
Use RUN --mount=type=cache,target=/var/cache/apt before apt-get update -> Option CQuick Check:
Cache apt downloads at /var/cache/apt = A [OK]
- Mounting wrong directories like /etc/apt or /usr/bin/apt
- Mounting cache after install instead of before update
- Confusing apt cache with config directories
Ready to prove it?
Pass the test and this pattern moves unknown
