Bird
Raised Fist0
Dockerdevops~5 mins

Cache mount for faster builds in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Building Docker images can be slow when repeating steps that don't change. Cache mounts let Docker save and reuse data between builds to speed up this process.
When you want to speed up installing dependencies that rarely change in your Docker build.
When compiling code that takes a long time but doesn't change often.
When running package managers like npm or pip inside Docker builds to avoid downloading packages every time.
When you want to keep build cache separate and avoid bloating your final image.
When you want to share cache between different build stages or builds on the same machine.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

# Use cache mount to speed up pip installs
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install requests flask

CMD ["python3"]

This Dockerfile uses a cache mount on the pip cache directory to save downloaded packages between builds.

FROM sets the base image.

RUN --mount=type=cache,target=/root/.cache/pip tells Docker to cache the pip download folder so future builds reuse it.

CMD sets the default command to run the Python interpreter.

Commands
Builds the Docker image named 'my-python-app' using the Dockerfile in the current folder. This step downloads and caches pip packages using the cache mount.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM python:3.11-slim ---> 3a2f3b4c5d6e Step 2/3 : RUN --mount=type=cache,target=/root/.cache/pip pip install requests flask ---> Running in 7a8b9c0d1e2f Collecting requests Downloading requests-2.28.1-py3-none-any.whl (62 kB) Collecting flask Downloading Flask-2.2.2-py3-none-any.whl (96 kB) Installing collected packages: requests, flask Successfully installed flask-2.2.2 requests-2.28.1 Removing intermediate container 7a8b9c0d1e2f ---> 4b5c6d7e8f90 Step 3/3 : CMD ["python3"] ---> Running in 1a2b3c4d5e6f Removing intermediate container 1a2b3c4d5e6f ---> 9f8e7d6c5b4a Successfully built 9f8e7d6c5b4a Successfully tagged my-python-app:latest
→
-t - Assigns a name and optionally a tag to the image
Rebuild the image again to see the cache mount speeding up the pip install step by reusing cached packages.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM python:3.11-slim ---> 3a2f3b4c5d6e Step 2/3 : RUN --mount=type=cache,target=/root/.cache/pip pip install requests flask ---> Running in 8f7e6d5c4b3a Requirement already satisfied: requests in /usr/local/lib/python3.11/site-packages (2.28.1) Requirement already satisfied: flask in /usr/local/lib/python3.11/site-packages (2.2.2) Removing intermediate container 8f7e6d5c4b3a ---> 1a2b3c4d5e6f Step 3/3 : CMD ["python3"] ---> Running in 0f9e8d7c6b5a Removing intermediate container 0f9e8d7c6b5a ---> 7b6a5c4d3e2f Successfully built 7b6a5c4d3e2f Successfully tagged my-python-app:latest
→
-t - Assigns a name and optionally a tag to the image
Key Concept

If you remember nothing else from this pattern, remember: cache mounts let Docker save and reuse data between builds to make them faster without bloating the final image.

Common Mistakes
Not using --mount=type=cache in RUN commands that download or build dependencies.
Without cache mounts, Docker downloads or rebuilds dependencies every time, making builds slow.
Add --mount=type=cache,target=PATH inside RUN commands to cache dependency folders.
Using cache mounts but targeting the wrong directory or a directory that changes often.
If the cache target directory changes frequently, the cache is invalidated and no speedup happens.
Target stable cache directories like package manager caches that don't change unless dependencies change.
Summary
Use --mount=type=cache in RUN commands to cache folders like package manager caches.
Build the Docker image once to create the cache, then rebuild to see faster installs.
Cache mounts speed up builds without adding extra layers or size to 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