【问题标题】:Cannot connect to azure nuget feed when creating a container创建容器时无法连接到 azure nuget feed
【发布时间】:2020-11-15 00:03:57
【问题描述】:

我一直试图让 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.net azure docker docker-compose azure-devops


    【解决方案1】:

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

    1,使用 dotnet cli 将凭据添加到 docker 文件中的 nuget 源。然后在构建参数(ie. --build-arg PAT=$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
    

    您可以参考我对 this similar 线程的回答,该线程在 Azure 管道的 docker 容器中使用 azure nuget 提要。

    【讨论】:

    • 感谢您的回复!我最终按照线程将登录详细信息放入 NuGet.config 文件并将其复制过来,结果按预期工作。
    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 2018-08-09
    • 2021-12-21
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多