简体   繁体   English

Docker - do.net 恢复无法连接到私人订阅源

[英]Docker - dotnet restore fails connection to private feeds

Calling do.net restore <project> from my Dockerfile is resulting in a NU1301: Unable to load the service index for source error.从我的 Dockerfile 调用do.net restore <project>会导致NU1301: Unable to load the service index for source I've been going through many of the suggested similar questions and continue to have issues.我一直在经历许多建议的类似问题并继续遇到问题。 Here is as much info about the things I've tried as I can provide:这是我所能提供的关于我尝试过的事情的尽可能多的信息:

  • Docker Engine has its DNS set to "8.8.8.8" Docker 引擎将其 DNS 设置为“8.8.8.8”
  • Using Linux containers使用 Linux 个容器
  • RUN ping google.com succeeds (so I can reach the inte.net) RUN ping google.com成功(所以我可以访问 inte.net)
  • Works perfectly fine hitting the nuget.org feednuget.org提要上工作得很好
  • The nuget.config file currently has credentials in it just to get this working nuget.config 文件目前有凭据,只是为了让它工作
    • This will be removed for a different approach once I get this working一旦我开始工作,这将被删除以采用不同的方法
    • These are the same credentials (username/PAT) that I use during development on my host machine这些是我在主机上开发期间使用的相同凭据(用户名/PAT)
  • RUN curl <nuget_feed_url> succeeds RUN curl <nuget_feed_url>成功
  • Running the restore command with --verbosity detailed doesn't provide any other error messages but the one使用--verbosity detailed运行 restore 命令不会提供任何其他错误消息,但会提供一条错误消息

Here is the section of the Dockerfile in question这是有问题的 Dockerfile 的部分

FROM mcr.microsoft.com/dotnet/aspnet:6.0.13 AS base
# Create dockeruser in base layer
RUN addgroup --system --gid 1000 dockergroup \
    && adduser --system --uid 1000 --ingroup dockergroup --shell /bin/sh dockeruser

WORKDIR /app
EXPOSE 8080

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
# Arguments are required in each stage in order to get the correct value

WORKDIR /src
COPY ["src/Nucleus.LumberYard.API/", "Nucleus.LumberYard.API/"]
COPY ["./nuget.config", "./nuget.config"]

WORKDIR "/src/Nucleus.LumberYard.API"
#COPY [".editorconfig", "./"]
RUN dotnet restore "Nucleus.LumberYard.API.csproj"
RUN dotnet build "Nucleus.LumberYard.API.csproj" -c Release --no-restore

Environment info环境信息

  • Docker Desktop v3.3.1 Docker桌面v3.3.1
  • Docker v20.10.5 Docker v20.10.5

Based on my understanding you have the following scenario:根据我的理解,您有以下情况:

  • a .NET 6 application with some references to nuget packages一个 .NET 6 应用程序,其中包含一些对 nuget 包的引用
  • some nuget packages are taken from the usual Nuget public repository, some others are taken from a private nuget feed一些 nuget 包取自通常的 Nuget 公共存储库,其他一些包取自私有 nuget 提要
  • you are distributing your application via a docker image and during the docker build process you want to run a do.net restore command targeting one of your csproj file您正在通过 docker 图像分发您的应用程序,并且在 docker 构建过程中您想要运行针对您的 csproj 文件之一的csproj do.net restore命令
  • the do.net restore command fails because the do.net cli is unable to talk with your private nuget feed do.net restore命令失败,因为do.net cli 无法与您的私有 nuget 提要通信

I encountered the very same situation with the project I'm working on.我在从事的项目中遇到了完全相同的情况。 We have a private Nuget feed hosted in Azure Devops and we too had some troubles figuring out how to solve this.我们有一个私有的 Nuget 提要托管在 Azure Devops 中,我们在解决这个问题时也遇到了一些麻烦。

First of all, let's clarify the root cause of the problem.首先,让我们弄清楚问题的根本原因。 You did the right thing veryfying that you are able to reach the nuget feed from your build machine, via the curl command you mentioned.你做了正确的事情,你可以通过你提到的curl命令从你的构建机器到达 nuget 提要。 What is actually failing is the authentication between your build machine and the private nuget feed.实际上失败的是您的构建机器和私有 nuget 提要之间的身份验证。

The first thing you need is a personal access token with read permissions for your nuget feed.您首先需要的是个人访问令牌,它具有您的 nuget 提要的读取权限 You can follow this guide to create the personal access token you need.您可以按照本指南创建所需的个人访问令牌。

Once you have the token, you need to provide it to the do.net cli.获得令牌后,您需要将其提供给do.net cli。 There are several ways to do so, I'm going to explain the one that worked for us.有几种方法可以做到这一点,我将解释对我们有用的一种。

Instead of adding the nuget source to the nuget.config file, we registers it via a cli command.我们不是将 nuget 源添加到nuget.config文件,而是通过 cli 命令注册它。 I'm quite sure there is a way to do exactly the same thing via the nuget.config file (see here for more details).我很确定有一种方法可以通过nuget.config文件做完全相同的事情(有关更多详细信息,请参见此处)。

This is the cli command we use inside of our docker file:这是我们在 docker 文件中使用的 cli 命令:

RUN dotnet nuget add source https://foo.bar.com/something/nuget/v3/index.json -u "whatever" -p "my-personal-access-token" --store-password-in-clear-text --valid-authentication-types "basic"

Notice that:请注意:

  • https://foo.bar.com/something/nuget/v3/index.json is the absolute URL pointing to the index of your private nuget feed https://foo.bar.com/something/nuget/v3/index.json是绝对 URL 指向您的私人 nuget 提要的索引
  • the username can be whatever you like.用户名可以是任何你喜欢的。 You do need to provide a value, but I didn't notice any difference even putting there a random string like whatever确实需要提供一个值,但我没有注意到whatever区别,即使在那里放一个随机字符串
  • the fictious value my-personal-access-token must be substituted with the personal access token you have created as a first step作为第一步,必须将虚构值my-personal-access-token替换为您创建的个人访问令牌

Here you can find the full reference for the do.net nuget add source command.您可以在此处找到do.net nuget add source命令的完整参考。

After registering this source with the do.net cli, you will be able to run your do.net restore command with no errors.使用do.net cli 注册此源后,您将能够无错误地运行do.net restore命令。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM