尝试访问 azure 工件时,.Net 6 docker 构建失败,并显示“错误 NU1301:无法加载源的服务索引”

use*_*413 6 docker-build .net-6.0

请注意,我尝试使用 docker 桌面在本地构建此文件,而不是在 Azure CI/CD 管道中构建。

我已经使用https://github.com/microsoft/artifacts-credprovider开始了这个过程,但这也不起作用,后来发现新的 dotnet 恢复可以完成它的工作。因此,参考https://github.com/microsoft/artifacts-credprovider/issues/220后尝试了以下方法

以下是我尝试过的步骤。

我已经生成了 PAT。

下面是我的 docker 文件。

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyProj.csproj", "."]

COPY . .
WORKDIR "/src/."

ARG PAT
ARG FEED_URL

ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
RUN dotnet nuget add source "${FEED_URL}" -u "noneed" -p "${PAT}" --store-password-in-clear-    text --valid-authentication-types basic

RUN dotnet restore "./MyProj.csproj"

RUN dotnet build "MyProj.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProj.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProj.dll"]
Run Code Online (Sandbox Code Playgroud)

然后我运行 docker build 如下

docker build . --build-arg FEED_URL=https://pkgs.dev.azure.com/[mycompanydata]/nuget/v3/index.json --build-arg PAT=secret
Run Code Online (Sandbox Code Playgroud)

在输出中,我可以看到 dotnet nuget add 源通过替换参数正确构建。但一段时间后错误显示

error NU1301: Unable to load the service index for source [my artifact feed url]
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 nuget.config 文件以及https://blog.devops.dev/consuming-private-nuget-feeds-from-a-dockerfile-in-a-secure-and-devops-Friendly-中提到的manner-b5c90ea90bba?gi=70f359a1d550这里唯一的更改是添加 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="opentrips" value="azure-artifact-feed-url"/>
      </packageSources>
    <packageSourceCredentials>
        <openTrips>
        <add key="Username" value="noneed" />
        <add key="ClearTextPassword" value="my-pat" />
        </openTrips>
      </packageSourceCredentials>
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后将恢复步骤更改为;

RUN dotnet restore "./OpenTrips.Services.UserProfile.csproj" --configfile nuget.config --ignore-failed-sources
Run Code Online (Sandbox Code Playgroud)

然后运行 ​​docker build 。这次出现同样的问题,但错误代码不是 NU1301,而是显示 NU1801。然后尝试Nuget 连接尝试失败“无法加载源的服务索引”,其中表示删除 %appData% 中的 nuget.config 相同的 NU1801 错误。

然后在其他一些来源中,他们提到要确保在 Edge 中启用 TLS 1.2,而且确实如此。然后将 dns 添加到 Docker Desktop 配置文件中,例如:"dns": [ "8.8.8.8","1.1.1.1" ],

除了上述之外,我还尝试了一些其他的方法,这些方法对某些人随机有效。基本上没有 nuget.config 我得到 NU1301 和 nuget.config NU1801。我完全迷失了。