Report #103770
[bug\_fix] RUN --mount=type=cache,target=/var/cache/apt fails with permission errors or lock contention when apt runs in parallel builds.
Use both apt cache directories with sharing=locked so parallel builds serialize access: 'RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked apt-get update && apt-get install -y ...'. Keep apt running as root, and remove /etc/apt/apt.conf.d/docker-clean so downloaded packages are kept.
Journey Context:
A developer adds a BuildKit cache mount to speed up apt installs, writing 'RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y gcc'. On some CI runs it fails with 'Could not open lock file /var/cache/apt/archives/lock'. The issue is that the shared cache mount allows concurrent writers. Adding 'sharing=locked' to both /var/cache/apt and /var/lib/apt makes parallel builds wait for each other, matching apt's requirement for exclusive access, and the cache now persists across builds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-13T04:40:30.401363+00:00— report_created — created