Bird
Raised Fist0
Dockerdevops~20 mins

Cache mount for faster builds in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache Mount Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Docker build with cache mount
What will be the output of the following Docker build command snippet when using cache mount for pip packages?
Docker
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
ADocker build fails with syntax error due to invalid mount option.
BDocker ignores the cache mount and installs packages from scratch every time.
CDocker uses the cache directory to speed up pip installs, reducing build time.
DDocker caches the entire image layer, not just pip cache, causing larger image size.
Attempts:
2 left
💡 Hint
Think about what the cache mount does for package managers during builds.
🧠 Conceptual
intermediate
1:30remaining
Purpose of cache mounts in Docker builds
Why are cache mounts used in Docker builds?
ATo persist data between build steps and speed up repeated operations like package installs.
BTo increase the size of the Docker image for better caching.
CTo share files between running containers at runtime.
DTo permanently store build artifacts inside the final image.
Attempts:
2 left
💡 Hint
Think about what slows down builds and how cache helps.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting cache mount syntax error
You run this Dockerfile line but get a syntax error: RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt. What is the likely cause?
ACache mounts are not supported on Linux hosts.
BThe cache mount target path is invalid and must be an absolute path inside /tmp.
CThe pip install command is incorrect and causes the error.
DThe Dockerfile is missing the syntax directive: <code># syntax=docker/dockerfile:1.4</code>.
Attempts:
2 left
💡 Hint
Check Dockerfile version and syntax requirements for cache mounts.
🔀 Workflow
advanced
2:30remaining
Optimizing Docker build with cache mounts
Which Dockerfile snippet best uses cache mounts to speed up npm package installation during build?
ARUN --mount=type=cache,target=/root/.npm npm install
BRUN npm install --cache /root/.npm
CRUN --mount=type=cache,target=/root/.cache/npm npm install
DRUN --mount=type=bind,target=/root/.cache/npm npm install
Attempts:
2 left
💡 Hint
Consider the correct cache directory for npm and mount type.
✅ Best Practice
expert
3:00remaining
Best practice for cache mount usage in multi-stage builds
In a multi-stage Docker build, where should you place cache mounts for package managers to maximize build speed and minimize final image size?
AUse cache mounts in every stage to ensure all layers cache package data.
BUse cache mounts only in the build stage where packages are installed, not in the final stage.
CUse cache mounts only in the final stage to keep cache data in the final image.
DAvoid cache mounts in multi-stage builds to prevent cache pollution.
Attempts:
2 left
💡 Hint
Think about where package installation happens and what ends up in the final image.

Practice

(1/5)
1. What is the main purpose of using --mount=type=cache in a Dockerfile during build?
easy
A. To save files between build steps and speed up future builds
B. To permanently store files in the final image
C. To share files between running containers
D. To mount host directories into the container at runtime

Solution

  1. Step 1: Understand cache mount purpose

    The --mount=type=cache option is used during build to save files temporarily between build steps.
  2. Step 2: Identify effect on build speed

    By saving files like package downloads, it avoids repeating work, making future builds faster.
  3. Final Answer:

    To save files between build steps and speed up future builds -> Option A
  4. Quick Check:

    Cache mount speeds builds = D [OK]
Hint: Cache mounts save build files to speed up next builds [OK]
Common Mistakes:
  • Confusing cache mount with runtime volume mount
  • Thinking cache mount stores files in final image
  • Assuming cache mount shares files between containers
2. Which of the following is the correct syntax to use a cache mount in a Dockerfile RUN command?
easy
A. RUN --mount=type=volume,target=/root/.cache npm install
B. RUN --cache=type=mount,target=/root/.cache npm install
C. RUN --mount=cache,target=/root/.cache npm install
D. RUN --mount=type=cache,target=/root/.cache npm install

Solution

  1. Step 1: Check correct mount syntax

    The correct syntax uses --mount=type=cache,target=... inside the RUN command.
  2. Step 2: Validate options

    RUN --mount=type=cache,target=/root/.cache npm install matches the correct syntax exactly; others have wrong keywords or types.
  3. Final Answer:

    RUN --mount=type=cache,target=/root/.cache npm install -> Option D
  4. Quick Check:

    Correct cache mount syntax = A [OK]
Hint: Use --mount=type=cache,target=... exactly in RUN [OK]
Common Mistakes:
  • Using --cache instead of --mount
  • Omitting type=cache
  • Using type=volume instead of cache
3. Given this Dockerfile snippet:
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?
medium
A. The second RUN command will be slower because cache is cleared
B. The second RUN command will be faster because it reuses cached files
C. The second RUN command will fail due to cache mount conflict
D. The second RUN command ignores cache and runs normally

Solution

  1. Step 1: Understand cache mount effect on repeated commands

    The cache mount stores files from the first pip install in /root/.cache.
  2. Step 2: Predict second RUN command behavior

    The second RUN reuses cached files, so it runs faster by skipping downloads.
  3. Final Answer:

    The second RUN command will be faster because it reuses cached files -> Option B
  4. Quick Check:

    Cache reuse speeds second build step = B [OK]
Hint: Cache mount speeds repeated installs by reusing files [OK]
Common Mistakes:
  • Assuming cache clears between RUN commands
  • Thinking cache causes build failure
  • Believing cache is ignored during build
4. You wrote this Dockerfile line:
RUN --mount=type=cache,target=/root/.cache npm install

But the cache is not speeding up builds. What is the likely mistake?
medium
A. The cache mount target directory is incorrect or not used by npm
B. You forgot to add --cache flag instead of --mount
C. Cache mounts only work with Python, not npm
D. You must run docker build with --no-cache option

Solution

  1. Step 1: Check cache mount target relevance

    Cache mount must target the directory where npm stores cache files, usually /root/.npm, not /root/.cache.
  2. Step 2: Understand npm cache usage

    If the target directory is wrong, npm won't use the cache, so builds stay slow.
  3. Final Answer:

    The cache mount target directory is incorrect or not used by npm -> Option A
  4. Quick Check:

    Correct cache target directory needed = C [OK]
Hint: Match cache target to tool's actual cache folder [OK]
Common Mistakes:
  • Using wrong directory for cache mount target
  • Confusing --cache and --mount flags
  • Thinking cache mounts only work for some languages
5. You want to speed up a Docker build that uses apt-get update and apt-get install. How can you use cache mounts to optimize this build?
hard
A. Use RUN --mount=type=cache,target=/etc/apt before apt-get update
B. Use RUN --mount=type=cache,target=/var/lib/apt after apt-get install
C. Use RUN --mount=type=cache,target=/var/cache/apt before apt-get update
D. Use RUN --mount=type=cache,target=/usr/bin/apt before apt-get update

Solution

  1. Step 1: Identify apt cache directory

    The apt package manager stores downloaded package files in /var/cache/apt.
  2. Step 2: Apply cache mount before update

    Mounting cache at /var/cache/apt before apt-get update saves downloaded package lists and speeds up future runs.
  3. Final Answer:

    Use RUN --mount=type=cache,target=/var/cache/apt before apt-get update -> Option C
  4. Quick Check:

    Cache apt downloads at /var/cache/apt = A [OK]
Hint: Cache package manager download folders to speed builds [OK]
Common Mistakes:
  • Mounting wrong directories like /etc/apt or /usr/bin/apt
  • Mounting cache after install instead of before update
  • Confusing apt cache with config directories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard