FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build # 修改源以加速包安装 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories WORKDIR /source # 先复制解决方案文件和项目文件,以确保它们在构建中保持不变 COPY *.sln ./ COPY aspnetapp/*.csproj ./aspnetapp/ # 恢复依赖 RUN dotnet restore -r linux-musl-x64 /p:PublishReadyToRun=true # 复制其余源代码并构建应用 COPY aspnetapp/. ./aspnetapp/ WORKDIR /source/aspnetapp RUN dotnet publish -c Release -o /app -r linux-musl-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true /p:PublishSingleFile=true # 最终阶段/image FROM mcr.microsoft.com/dotnet/runtime-deps:6.0-alpine-amd64 # 设置时区和安装证书 RUN apk add --no-cache tzdata ca-certificates && \ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone WORKDIR /app COPY --from=build /app ./ ENTRYPOINT ["./aspnetapp"]