在打包镜像时,由于程序中使用了本地的NuGet包,发现怎么打包都是失败,错误如下
我的dockerfile 文件如下
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
# 此阶段用于生成服务项目
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["src/YiAim.Shop.Web/YiAim.Shop.Web.csproj", "src/YiAim.Shop.Web/"]
COPY ["src/YiAim.Shop.Application/YiAim.Shop.Application.csproj", "src/YiAim.Shop.Application/"]
xxx
COPY ["src/YiAim.Shop.HttpApi/YiAim.Shop.HttpApi.csproj", "src/YiAim.Shop.HttpApi/"]
COPY ["src/YiAim.Shop.EntityFrameworkCore/YiAim.Shop.EntityFrameworkCore.csproj", "src/YiAim.Shop.EntityFrameworkCore/"]
RUN dotnet restore "src/YiAim.Shop.Web/YiAim.Shop.Web.csproj"
COPY . .
WORKDIR "/src/src/YiAim.Shop.Web"
RUN dotnet build "YiAim.Shop.Web.csproj" -c Release -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "YiAim.Shop.Web.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "YiAim.Shop.Web.dll"]
无法找到本地引用的NuGet包
解决方案
1、将本地NuGet网络化(有点麻烦)
2、将本地NuGet包加入打包环境
本地NuGet包加入打包环境,搞了好久才弄明白~~
- 在
NuGet.Config
文件中,配置的NuGet 源(没有这个文件新建即可,NuGet.Config 文件与 .sln 文件同一文件夹中)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="./Nupkg" />
</packageSources>
</configuration>
- 在dockerfile 文件中加入相关配置
# NuGet.Config里面要声明本地NuGet包的所在位置(使用相对路径)
COPY ./NuGet.Config .
# 复制本地的Nupkg包,加入编译环境,不然找不到本地包会报错 ./Nupkg/==/src/Nupkg/
COPY ./Nupkg ./Nupkg/
# 添加nuget本地来源
RUN dotnet nuget add source /src/Nupkg/
ps:本地的包注意存放的位置,我就是在这里花费了好多时间
可以参考我这,完整的dockerfile 与目录,灵活配置
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# NuGet.Config里面要声明本地NuGet包的所在位置(使用相对路径)
COPY ./NuGet.Config .
# 复制本地的Nupkg包,不然找不到本地包会报错 ./Nupkg/==/src/Nupkg/ (WORKDIR /src已经设置了基本路径)
COPY ./Nupkg ./Nupkg/
# 添加nuget本地来源
RUN dotnet nuget add source /src/Nupkg/
COPY ["src/YiAim.Shop.Web/YiAim.Shop.Web.csproj", "src/YiAim.Shop.Web/"]
COPY ["src/YiAim.Shop.Application/YiAim.Shop.Application.csproj", "src/YiAim.Shop.Application/"]
COPY ["src/YiAim.Shop.Auth/YiAim.Shop.Auth.csproj", "src/YiAim.Shop.Auth/"]
COPY ["src/YiAim.Shop.Domain.Shared/YiAim.Shop.Domain.Shared.csproj", "src/YiAim.Shop.Domain.Shared/"]
COPY ["src/YiAim.Shop.Domain/YiAim.Shop.Domain.csproj", "src/YiAim.Shop.Domain/"]
COPY ["src/YiAim.Shop.Application.Contracts/YiAim.Shop.Application.Contracts.csproj", "src/YiAim.Shop.Application.Contracts/"]
COPY ["src/YiAim.Shop.Virtually.Recharge/YiAim.Shop.Virtually.Recharge.csproj", "src/YiAim.Shop.Virtually.Recharge/"]
COPY ["src/YiAim.Shop.Wechat/YiAim.Shop.Wechat.csproj", "src/YiAim.Shop.Wechat/"]
COPY ["src/YiAim.Shop.HttpApi.Admin/YiAim.Shop.HttpApi.Admin.csproj", "src/YiAim.Shop.HttpApi.Admin/"]
COPY ["src/YiAim.Shop.HttpApi/YiAim.Shop.HttpApi.csproj", "src/YiAim.Shop.HttpApi/"]
COPY ["src/YiAim.Shop.EntityFrameworkCore/YiAim.Shop.EntityFrameworkCore.csproj", "src/YiAim.Shop.EntityFrameworkCore/"]
RUN dotnet restore "src/YiAim.Shop.Web/YiAim.Shop.Web.csproj"
COPY . .
WORKDIR "/src/src/YiAim.Shop.Web"
RUN dotnet build "YiAim.Shop.Web.csproj" -c Release -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "YiAim.Shop.Web.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "YiAim.Shop.Web.dll"]