首页 > 解决方案 > 创建容器时无法连接到 azure nuget feed

问题描述

我一直试图让 docker 从托管在 Azure Devops 中的私有 nuget 提要中提取,但我一直收到 401 未经授权的错误。我查看了许多其他答案和资源,但所有这些都没有帮助。我的 dockerfile 脚本如下

# escape=`

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /src
EXPOSE 80
EXPOSE 443

ARG FEED_URL
ARG FEED_ACCESSTOKEN

# Install powershell via dotnet
RUN dotnet tool install --global PowerShell --version 7.0.0
SHELL ["pwsh", "-command"]

## CURRENT ERROR - "-AddNetfx" - NOT CONSIDERED AN ARGUMENT, ALTHOUGH IT IS (CAN BE SEEN IN THE SCRIPT ENTRY) ##
# Install credprovider which is required for accessing private artifact repository
RUN Invoke-WebRequest https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1 -OutFile installcredprovider.ps1; `
    .\installcredprovider.ps1 -AddNetfx; `
    del installcredprovider.ps1
#################################################################################################################

# Copy csproj and sln files
COPY ["Abstractions/*.csproj", "./Abstractions/"]
COPY ["AdminServicesPortal/*.csproj", "./AdminServicesPortal/"]
COPY ["DataInterface/*.csproj", "./DataInterface/"]
COPY ["Resources/*.csproj", "./Resources/"]

# Need to copy over docker NuGet packages
COPY "AdminServicesPortal/nuget.config" "AdminServicesPortal/"

# Enable session token cache
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true


ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS `
    "{`"endpointCredentials`": [{`"endpoint`":`"https://pkgs.dev.azure.com/ORG/PROJECT/_packaging/FEED/nuget/v3/index.json`", `"username`":`"USER`", `"password`":`"PAT`"}]}"
RUN echo $Env:VSS_NUGET_EXTERNAL_FEED_ENDPOINTS

# Restore dependencies before copying everything else over
RUN dotnet restore "AdminServicesPortal/Portal.csproj"

# Copy the rest of the files required for a build
# Note that this is separate, because we should stop building
# the container if we do not have all required dependencies
COPY Abstractions/. ./Abstractions/
COPY AdminServicesPortal/. ./AdminServicesPortal/
COPY DataInterface/. ./DataInterface/
COPY Resources/. ./Resources/

WORKDIR "/src/AdminServicesPortal"
RUN dotnet build "Portal.csproj" -c Release -o /app/build

# Publish application
FROM build-env AS publish
RUN dotnet publish "Portal.csproj" -c Release -o /app/publish

# Runtime environment for container
FROM build-env AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MPX.AdminServices.Portal.dll"]

我从 Visual Studio 得到的错误如下

2>  Failed to restore C:\src\Resources\Resources.csproj (in 2.99 sec).
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/{ORG}/{PROJECT}/_packaging/{FEED}/nuget/v3/index.json. [C:\src\AdminServicesPortal\Portal.csproj]
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error :   Response status code does not indicate success: 401 (Unauthorized). [C:\src\AdminServicesPortal\Portal.csproj]

从我在下面的链接中可以看出;我的 dockerfile 脚本应该可以工作

https://github.com/dotnet/dotnet-docker/blob/master/documentation/scenarios/nuget-credentials.md#using-the-azure-artifact-credential-provider

为了确保没有访问权限的用户的 PAT 不是问题,我让他们访问整个组织中的所有内容。任何帮助将不胜感激!

注意#1:我目前的怀疑是我可能需要传递一些额外的参数,因为私有 nuget 提要是针对一个项目而不是整个组织的?

注意 #2:我运行了以下 PowerShell 命令(https://docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops#package-and-publish -the-module ) 工作正常,所以我对为什么我的 dockerfile 脚本不起作用感到更加困惑。

nuget sources Add -Name "PowershellModules" -Source "https://pkgs.dev.azure.com/<org_name>/_packaging/<feed_name>/nuget/v3/index.json" -username "<user_name>" -password "<personal_access_token(PAT)>"

标签: asp.netazuredockerdocker-composeazure-devops

解决方案


您可以在不使用 NuGet 凭据插件的情况下查看以下解决方法。

1,使用 dotnet cli 将凭据添加到 docker 文件中的 nuget 源。然后在构建参数中传递 $PAT (ie. --build-arg PAT=$PAT )

ARG PAT
COPY . .
RUN dotnet nuget add source "your-source-url" --name "source-name" --username "useless" --password "$PAT" --store-password-in-clear-text
RUN dotnet restore

2,您也可以尝试使用 nuget 命令将凭据添加到配置文件中。并在构建上下文中包含具有凭据的 nuget.config。并将 nuget.config 复制到您的 docker 文件中。见下文:

sources Add -Name "MyPackages" -Source "https://my.pkgs.visualstudio.com/_packaging/MyPackages/nuget/v3/index.json" -username any -password $PAT -ConfigFile Source/Nuget.config -StorePasswordInClearText

复制docker文件中的nuget.config,恢复完成后可以删除nuget.config文件:

COPY *.csproj .
COPY ./nuget.config .
RUN dotnet restore
RUN rm nuget.config

您可以参考我对在 Azure 管道的 docker 容器中使用 azure nuget feed 的类似线程的回答。


推荐阅读