You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建时dotnet restore失败:NU1301无法加载NuGet源服务索引

解决Docker构建时NU1301无法加载NuGet源的问题

检查Docker网络配置

  • 执行docker network ls确认默认网络状态,尝试用宿主机网络构建:docker build --network host .,绕过容器内部网络限制
  • 如果是DNS解析问题,可在Dockerfile中指定公共DNS:
    RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf
    

验证容器内网络连通性

  • 临时修改Dockerfile,在dotnet restore前添加网络测试命令,排查连通性:
    RUN ping api.nuget.org -c 4
    RUN curl https://api.nuget.org/v3/index.json
    
    根据输出判断是DNS问题还是HTTPS访问受阻

调整NuGet源与基础镜像

  • 在项目根目录创建NuGet.Config,添加国内镜像源:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        <add key="Tsinghua" value="https://mirrors.tuna.tsinghua.edu.cn/nuget/" />
      </packageSources>
    </configuration>
    
    然后在Dockerfile中复制并指定该配置:
    COPY NuGet.Config .
    RUN dotnet restore --configfile NuGet.Config YourProject.csproj
    
  • 更换为最新版基础镜像,比如mcr.microsoft.com/dotnet/sdk:7.0-cbl-mariner2.0,避免旧镜像的网络组件问题

清理Docker构建缓存

  • 执行docker builder prune清理缓存,再重新执行docker build .,确保使用全新构建环境

排查宿主机代理残留

  • 执行echo $HTTP_PROXY和echo $HTTPS_PROXY检查是否有残留代理变量,若有则清空后再构建:
    unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy
    docker build .
    

内容的提问来源于stack exchange,提问作者Mohaned Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:52:32