diff --git a/src/InterfaceForward.Api/Dockerfile b/src/InterfaceForward.Api/Dockerfile index 75a7145..969c197 100644 --- a/src/InterfaceForward.Api/Dockerfile +++ b/src/InterfaceForward.Api/Dockerfile @@ -1,16 +1,24 @@ -FROM mcr.microsoft.com/dotnet/sdk:8.0.421 AS base -ARG BUILD_CONFIGURATION=Release +# ===== 阶段1: 基础依赖层(可缓存) ===== +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src -COPY . /src + +# ✅ 关键优化:只复制 .csproj 文件,让 restore 层可以缓存 +COPY ["src/InterfaceForward.Api/InterfaceForward.Api.csproj", "src/InterfaceForward.Api/"] RUN dotnet restore "src/InterfaceForward.Api/InterfaceForward.Api.csproj" + +# ===== 阶段2: 构建层 ===== +# ✅ 现在才复制所有代码(这一层变动频繁,但 restore 已经跑完了) +COPY . . WORKDIR "/src/src/InterfaceForward.Api" +ARG BUILD_CONFIGURATION=Release RUN dotnet build "InterfaceForward.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build -FROM base AS publish +# ===== 阶段3: 发布层 ===== +FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "InterfaceForward.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false -# 使用运行时镜像 +# ===== 阶段4: 最终运行时(轻量级) ===== FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime WORKDIR /app COPY --from=publish /app/publish .