Sub topic 5
Cache ids, sharing modes, and ownership
This chapter assumes you can write a RUN --mount=type=cache instruction and know that the cache directory is kept outside the image layers. Here you will control three things: which cache a step uses, what happens when two builds use it at once, and who owns the files in it.
The cache id
Every cache mount belongs to a named cache. The id option sets that name. If you leave it out, BuildKit uses the target path as the id.
This has two effects:
- Two
RUNsteps with the sametargetshare one cache, even in different stages. - Two steps with different targets get separate caches, even when they use the same package manager.
Set an explicit id when you want to share a cache across different target paths, or keep two caches apart that happen to use the same path.
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS deps
COPY requirements.txt .
RUN --mount=type=cache,id=pip-cache,target=/root/.cache/pip \
pip install -r requirements.txt
FROM python:3.12-slim AS test
COPY requirements-dev.txt .
RUN --mount=type=cache,id=pip-cache,target=/root/.cache/pip \
pip install -r requirements-dev.txtBoth stages read and write the pip-cache cache, so a wheel downloaded in one stage is reused in the other.
Sharing modes
The sharing option decides what happens when several builds, or several parallel steps in one build, want the same cache at the same moment.
| Mode | Behavior | Use it when |
|---|---|---|
| `shared` (default) | All users read and write the same directory at once. | The tool tolerates concurrent access, such as the Go build cache. |
| `private` | If the cache is busy, BuildKit gives the second user a new, empty cache. | You want isolation and accept a cold start. |
| `locked` | The second user waits until the first one finishes. | The tool corrupts its cache under concurrent writes, such as `apt`. |
Waiting costs time, so use locked only where corruption is a real risk. For apt it is the standard choice, as the recipes later in this tutorial show.
Ownership and permissions
BuildKit creates a cache directory owned by root with mode 0755. That is fine while the step runs as root. If an earlier USER instruction switched to a non-root user, the tool cannot write to the cache, and the step fails with a permission error or silently skips caching.
Fix this with the uid, gid, and mode options:
# syntax=docker/dockerfile:1
FROM node:20-slim
RUN mkdir /app && chown node:node /app
WORKDIR /app
USER node
COPY --chown=node:node package*.json ./
RUN --mount=type=cache,id=npm-cache,target=/home/node/.npm,uid=1000,gid=1000,sharing=locked \
npm ciThe official node image runs its node user as uid 1000. The cache is created with that owner, so npm can write to /home/node/.npm. Note that the target is the non-root user's home directory, not /root/.npm.
Other mount options worth knowing
modesets the permission bits of the cache directory, for examplemode=0777. Preferuidandgidover loosening the mode.fromandsourceseed the cache from another stage or image instead of starting empty. Most builds do not need them.
Common mistakes
- Using
sharing=privateand wondering why the cache never warms. Each concurrent build gets a fresh cache, so little is reused. - Leaving the default
sharedmode forapt. Two parallel builds can corrupt/var/lib/apt. - Forgetting
uidandgidafter aUSERinstruction. - Reusing one
idfor tools that store incompatible data in the same directory.
Exercise
A Dockerfile has USER app (uid 1001) followed by RUN --mount=type=cache,target=/home/app/.cache/pip pip install -r requirements.txt. The install fails with a permission error on the cache directory. What do you change, and which sharing mode would you add if two CI jobs build this image on the same builder at once?
Show answer
Add uid=1001,gid=1001 to the mount options so the cache directory is owned by the app user. The default shared mode is usually safe for pip, so no change is needed. sharing=locked is a cautious option if you see corruption, at the cost of making the second job wait.