From 5c5a21852b2f5eb181aacabc82e43b35fcfe505d Mon Sep 17 00:00:00 2001 From: Jos Nienhuis <6952249+joszz@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:06:46 +0100 Subject: [PATCH] fix(docker): NuGet cache mount breaks parallel and pruned builds Two problems with the cache mount from #836: - Compose builds the API and Worker at once, and both restored into the shared `nuget` mount. NuGet keeps its extraction locks in each container's own /tmp, so the two restores deleted each other's temp files: "Could not find file '/root/.nuget/packages/serilog/4.4.0/ zzhu1xit.trf'". sharing=locked makes them take turns. - BuildKit's layer cache ignores the mount, so the restore layer can be reused after the mount was evicted or pruned. `publish --no-restore` then failed with NETSDK1064. publish now restores itself, which is a quick check when the packages are already there. Reproduced both with `docker compose -f docker-compose.yml -f docker-compose.build.yml build --no-cache api worker` and a fresh mount id: 4 of 4 rounds failed before, 4 of 4 pass after. Co-Authored-By: Claude Opus 5.5 --- docker/all-in-one/Dockerfile | 13 ++++++++----- src/GarageStack.Api/Dockerfile | 11 +++++++---- src/GarageStack.Worker/Dockerfile | 11 +++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docker/all-in-one/Dockerfile b/docker/all-in-one/Dockerfile index fb628cc8..3bd3ca2a 100644 --- a/docker/all-in-one/Dockerfile +++ b/docker/all-in-one/Dockerfile @@ -45,8 +45,11 @@ COPY src/GarageStack.Data/GarageStack.Data.csproj GarageStack.Data/ COPY src/GarageStack.Api/GarageStack.Api.csproj GarageStack.Api/ COPY src/GarageStack.Worker/GarageStack.Worker.csproj GarageStack.Worker/ # The NuGet cache lives in a BuildKit cache mount, so a local rebuild after a package change -# only downloads what changed. The packages are not in any layer, so publish mounts it too. -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ +# only downloads what changed. sharing=locked makes parallel builds take turns (compose builds +# the API and Worker at once): NuGet keeps its locks in each container's own /tmp, so two +# restores into one shared folder delete each other's files. publish restores again rather than +# rely on the restore layer, because BuildKit can keep that layer after it evicts the mount. +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ dotnet restore GarageStack.Api/GarageStack.Api.csproj \ && dotnet restore GarageStack.Worker/GarageStack.Worker.csproj COPY src/GarageStack.Core/ GarageStack.Core/ @@ -54,9 +57,9 @@ COPY src/GarageStack.Data/ GarageStack.Data/ COPY src/GarageStack.Api/ GarageStack.Api/ COPY src/GarageStack.Worker/ GarageStack.Worker/ # ${APP_VERSION#v} strips a leading "v" (e.g. a tag name), which MinVer rejects -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ - dotnet publish GarageStack.Api/GarageStack.Api.csproj --no-restore -c Release -o /publish/api /p:MinVerVersionOverride=${APP_VERSION#v} \ - && dotnet publish GarageStack.Worker/GarageStack.Worker.csproj --no-restore -c Release -o /publish/worker /p:MinVerVersionOverride=${APP_VERSION#v} +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ + dotnet publish GarageStack.Api/GarageStack.Api.csproj -c Release -o /publish/api /p:MinVerVersionOverride=${APP_VERSION#v} \ + && dotnet publish GarageStack.Worker/GarageStack.Worker.csproj -c Release -o /publish/worker /p:MinVerVersionOverride=${APP_VERSION#v} # ── Stage 3: Pull SAIC MQTT Gateway (source of truth for the gateway app) ──── # The gateway is not on PyPI. We copy its pre-built venv + source directly from diff --git a/src/GarageStack.Api/Dockerfile b/src/GarageStack.Api/Dockerfile index 30fa3e81..a1d87560 100644 --- a/src/GarageStack.Api/Dockerfile +++ b/src/GarageStack.Api/Dockerfile @@ -9,16 +9,19 @@ COPY ["src/GarageStack.Core/GarageStack.Core.csproj", "GarageStack.Core/"] COPY ["src/GarageStack.Data/GarageStack.Data.csproj", "GarageStack.Data/"] COPY ["src/GarageStack.Api/GarageStack.Api.csproj", "GarageStack.Api/"] # The NuGet cache lives in a BuildKit cache mount, so a local rebuild after a package change -# only downloads what changed. The packages are not in any layer, so publish mounts it too. -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ +# only downloads what changed. sharing=locked makes parallel builds take turns (compose builds +# the API and Worker at once): NuGet keeps its locks in each container's own /tmp, so two +# restores into one shared folder delete each other's files. publish restores again rather than +# rely on the restore layer, because BuildKit can keep that layer after it evicts the mount. +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ dotnet restore "GarageStack.Api/GarageStack.Api.csproj" COPY src/GarageStack.Core/ GarageStack.Core/ COPY src/GarageStack.Data/ GarageStack.Data/ COPY src/GarageStack.Api/ GarageStack.Api/ WORKDIR /src/GarageStack.Api # ${APP_VERSION#v} strips a leading "v" (e.g. a tag name), which MinVer rejects -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ - dotnet publish --no-restore -c Release -o /app /p:MinVerVersionOverride=${APP_VERSION#v} +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ + dotnet publish -c Release -o /app /p:MinVerVersionOverride=${APP_VERSION#v} # Alpine with the "extra" globalization set: full ICU data (the API answers in en, nl, es and # pt) and tzdata (climate schedules keep their start time in an IANA zone) are built in. About diff --git a/src/GarageStack.Worker/Dockerfile b/src/GarageStack.Worker/Dockerfile index 7be6219c..5136d88c 100644 --- a/src/GarageStack.Worker/Dockerfile +++ b/src/GarageStack.Worker/Dockerfile @@ -9,16 +9,19 @@ COPY ["src/GarageStack.Core/GarageStack.Core.csproj", "GarageStack.Core/"] COPY ["src/GarageStack.Data/GarageStack.Data.csproj", "GarageStack.Data/"] COPY ["src/GarageStack.Worker/GarageStack.Worker.csproj", "GarageStack.Worker/"] # The NuGet cache lives in a BuildKit cache mount, so a local rebuild after a package change -# only downloads what changed. The packages are not in any layer, so publish mounts it too. -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ +# only downloads what changed. sharing=locked makes parallel builds take turns (compose builds +# the API and Worker at once): NuGet keeps its locks in each container's own /tmp, so two +# restores into one shared folder delete each other's files. publish restores again rather than +# rely on the restore layer, because BuildKit can keep that layer after it evicts the mount. +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ dotnet restore "GarageStack.Worker/GarageStack.Worker.csproj" COPY src/GarageStack.Core/ GarageStack.Core/ COPY src/GarageStack.Data/ GarageStack.Data/ COPY src/GarageStack.Worker/ GarageStack.Worker/ WORKDIR /src/GarageStack.Worker # ${APP_VERSION#v} strips a leading "v" (e.g. a tag name), which MinVer rejects -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ - dotnet publish --no-restore -c Release -o /app /p:MinVerVersionOverride=${APP_VERSION#v} +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ + dotnet publish -c Release -o /app /p:MinVerVersionOverride=${APP_VERSION#v} # Alpine with the "extra" globalization set: full ICU data (notifications are translated) and # tzdata (climate schedules keep their start time in an IANA zone) are built in. About 37 MB