Skip to content

Commit a577b86

Browse files
committed
Add "sharing=locked" to Apt cache example
I was wondering how using --mount=type=cache for Apt data would work on a CI server where builds might happen in parallel, considering that just two parallel Apt commands on a desktop system won't work. I found my answers in #1662 and #455, and thought it might be useful to gather them in the reference for future users. Signed-off-by: Fiona Klute <fiona.klute@gmx.de>
1 parent b44b941 commit a577b86

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

frontend/dockerfile/docs/reference.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,18 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
894894
# syntax=docker/dockerfile:1
895895
FROM ubuntu
896896
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
897-
RUN --mount=type=cache,target=/var/cache/apt \
898-
--mount=type=cache,target=/var/lib/apt \
897+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
898+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
899899
apt update && apt-get --no-install-recommends install -y gcc
900900
```
901901

902+
Apt needs exclusive access to its data, so the caches use the option
903+
`sharing=locked`, which will make sure multiple parallel builds using
904+
the same cache mount will wait for each other and not access the same
905+
cache files at the same time. You could also use `sharing=private` if
906+
you prefer to have each build create another cache directory in this
907+
case.
908+
902909
### RUN --mount=type=tmpfs
903910

904911
This mount type allows mounting tmpfs in the build container.

0 commit comments

Comments
 (0)