需求:将之前使用 ASP.NET Core 6.0 的项目打包成 Docker 镜像,然后在路由器(openwrt)上面运行,在执行打包成 docker 镜像的时候,在 dotnet restore 还原 nuget 包的时候会出现超时错误,如下图:
Determining projects to restore...
Failed to download package 'SkiaSharp.2.80.3' from 'https://api.nuget.org/v3-flatcontainer/skiasharp/2.80.3/skiasharp.2.80.3.nupkg'.
The download of 'https://api.nuget.org/v3-flatcontainer/skiasharp/2.80.3/skiasharp.2.80.3.nupkg' timed out because no data was received for 60000ms.
Exception of type 'System.TimeoutException' was thrown.
Failed to download package 'SkiaSharp.NativeAssets.Linux.2.80.3' from 'https://api.nuget.org/v3-flatcontainer/skiasharp.nativeassets.linux/2.80.3/skiasharp.nativeassets.linux.2.80.3.nupkg'.
The download of 'https://api.nuget.org/v3-flatcontainer/skiasharp.nativeassets.linux/2.80.3/skiasharp.nativeassets.linux.2.80.3.nupkg' timed out because no data was received for 60000ms.
Exception of type 'System.TimeoutException' was thrown.
项目 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/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["HomeCloud/HomeCloud.csproj", "HomeCloud/"]
RUN dotnet restore "HomeCloud/HomeCloud.csproj"
COPY . .
WORKDIR "/src/HomeCloud"
RUN dotnet build "HomeCloud.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "HomeCloud.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HomeCloud.dll"]
打包成 docker 镜像命令如下:
docker build -t itsvse_test:v1 -f /data/ddns/HomeCloud/Dockerfile .
通过搜索引擎发现其他用户也遇到类似的问题,链接:https://github.com/NuGet/Home/issues/10275
根据网友的回答,尝试增加 --disable-parallel 参数禁用并行,修改后如下:
RUN dotnet restore "HomeCloud/HomeCloud.csproj" --disable-parallel
现还是会出现 System.TimeoutException 超时的问题,突然灵机一动,既然是还原 nuget 包超时,为什么不能换个源呢?果断换成腾讯的 nuget 源,参考如下:
使用腾讯云 Nuget 包管理器加速下载
https://www.itsvse.com/thread-7875-1-1.html
nuget 国内源(镜像源)
https://www.itsvse.com/thread-3925-1-1.html
修改 Dockerfile 文件,移除默认的 nuget.org,新增腾讯云的源,如下:
RUN dotnet nuget remove source nuget.org
RUN dotnet nuget add source https://mirrors.cloud.tencent.com/nuget/ -n tencent_nuget
RUN dotnet restore "HomeCloud/HomeCloud.csproj"
最后,成功打包成 docker 镜像,在还原 nuget 包耗时 22 秒,没有出现超时问题,如下图:
(完)