.net core 打包docker 镜像

    科技2025-08-07  5

    一、新建项目:Linux.Web

    二、添加docker file

    三、将Dockerfile 移动到外层目录

     由于项目使用了私仓,所以添加一行:

    COPY ["nuget.config", "/src"]

    完事的Dockerfile 内容: 

    #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR /src COPY ["Linux.Web/Linux.Web.csproj", "Linux.Web/"] COPY ["nuget.config", "/src"] RUN dotnet restore "Linux.Web/Linux.Web.csproj" COPY . . WORKDIR "/src/Linux.Web" RUN dotnet build "Linux.Web.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "Linux.Web.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Linux.Web.dll"]

     四、添加nuget.config

    私仓: <add key="saas" value="http://192.168.1.108:71/nuget" />

     

    完整的nuget.config内容: 

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- defaultPushSource key works like the 'defaultPushSource' key of NuGet.Config files. --> <!-- This can be used by administrators to prevent accidental publishing of packages to nuget.org. --> <config> <add key="defaultPushSource" value="https://contoso.com/packages/" /> </config> <!-- Default Package Sources; works like the 'packageSources' section of NuGet.Config files. --> <!-- This collection cannot be deleted or modified but can be disabled/enabled by users. --> <packageSources> <add key="saas" value="http://192.168.1.108:71/nuget" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> </packageSources> <!-- Default Package Sources that are disabled by default. --> <!-- Works like the 'disabledPackageSources' section of NuGet.Config files. --> <!-- Sources cannot be modified or deleted either but can be enabled/disabled by users. --> </configuration>

    五、拷贝到linux 服务器:/data/wwwroot

    六、打包镜像

    docker build -t linux.web:v1.0.1 .

    Processed: 0.011, SQL: 8