0
0
Dockerdevops~5 mins

Cache mount for faster builds in Docker - Commands & Configuration

Choose your learning style9 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.