md-platform

build.md
View raw Back to list

PicTv.Api — Dockerfile Build Optimization Changes

Summary

The api/PicTv.Api/Dockerfile was updated to fix Docker layer caching issues that caused slow builds. Most rebuilds were re-downloading NuGet packages and re-copying the entire repository even when only source code changed.

What Changed

Before

COPY ["api/PicTv.Api/PicTv.Api.csproj", "api/PicTv.Api/"]
RUN dotnet restore "api/PicTv.Api/PicTv.Api.csproj"
COPY . .

After

# 1. Copy all .csproj files in the dependency chain
COPY ["api/PicTv.Api/PicTv.Api.csproj", "api/PicTv.Api/"]
COPY ["core/PicTv.Application/PicTv.Application.csproj", "core/PicTv.Application/"]
COPY ["core/PicTv.Infrastructure/PicTv.Infrastructure.csproj", "core/PicTv.Infrastructure/"]
COPY ["core/PicTv.Domain/PicTv.Domain.csproj", "core/PicTv.Domain/"]

# 2. Restore with NuGet cache mount
RUN --mount=type=cache,target=/root/.nuget/packages \
    dotnet restore "api/PicTv.Api/PicTv.Api.csproj"

# 3. Copy only the source directories needed for this build
COPY ["api/PicTv.Api/", "api/PicTv.Api/"]
COPY ["core/", "core/"]

Changes Explained

# Change Why
1 Copy all 4 .csproj files before restore dotnet restore needs the full ProjectReference chain to resolve dependencies. Without them the restore layer is invalidated on every build.
2 --mount=type=cache,target=/root/.nuget/packages on restore, build, and publish steps Persists the NuGet package cache across builds. Even when the restore layer is invalidated (e.g., a new package is added), already-downloaded packages are reused from the cache mount instead of re-downloaded.
3 Replace COPY . . with selective COPY of api/PicTv.Api/ and core/ Changes to workers/, tests/, or other unrelated directories no longer invalidate the build cache.
4 --no-install-recommends on apt-get install in the final stage Skips optional apt packages, reducing final image size.

Requirements

Project Dependency Chain

PicTv.Api
├── PicTv.Application
│   └── PicTv.Domain
└── PicTv.Infrastructure

If a new ProjectReference is added to any of these projects, the corresponding .csproj must also be copied in the Dockerfile before the restore step.

Risk


Please review these changes and confirm they are compatible with our CI/CD environment. Specifically:

  1. Is BuildKit enabled in our pipeline?
  2. Are there any concerns with the cache mount approach?
  3. Any other feedback before we merge?