.netdockerdotnet-restore

Unable to `dotnet restore` while building Docker image connecting to a private Azure DevOps NuGet feed


I have a .NET7 webapi project that is being built as a Docker container. The project is referencing NuGet package(s) that are published to a private Azure DevOps (on-prem hosted) NuGet feed. Project contains a NuGet.config file where this feed is defined and credentials for the feed. Credentials are a Personal Access Token that was generated through Azure DevOps UI.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />  
    <add key="MyFeed" value="https://.../nuget/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <MyFeed>
      <add key="Username" value="docker" />
      <add key="ClearTextPassword" value="PAT_generated_in_devops" />
    </MyFeed>
  </packageSourceCredentials>
</configuration>

Dockerfile contents:

FROM mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17 AS build
WORKDIR /src

# Copying all .csproj files here
# ...

COPY ./NuGet.config .

RUN dotnet restore "HttpApi.csproj" -r linux-x64

RUN dotnet publish "HttpApi.csproj" -c Release -r linux-x64 --self-contained true --no-restore -v:m -o /app -p:PublishReadyToRunComposite=true -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true

FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine

WORKDIR /app

COPY --from=build /app .

EXPOSE 8080

ENTRYPOINT [ "HttpApi" ]

Executing Dockerfile command that contains RUN dotnet restore ProjectName.csproj -r linux-x64 fails with error message: error NU1301: Unable to load the service index for source https://.../nuget/v3/index.json

I tried setting up my domain credentials as packageSourceCredentials for the NuGet feed. I tried setting HTTP_PROXY and HTTPS_PROXY as docker build arguments. I tried installing a local version of Azure DevOps Express 2022 to eliminate networking issues and connecting to the local NuGet feed. None of these solved the issue.

EDIT 1:
- NOTE 1: It's worth noting that dotnet restore works normally when run through Windows PowerShell or through Visual Studio. Issue occurrs when the project is being built by Docker.
- NOTE 2: Docker engine is utilizing WSL


Solution

  • The only solution that I managed to get to work was utilizing artifacts credentials provider as stated here: Artifacts credentials provider documentation. I also had to explicitly set -s FEED_URI argument on the dotnet restore command, although the source is also defined in the nuget.config file.

    EDIT 1
    Solution consists of:

    1. specify package source in nuget.config file located in the solution root
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />  
        <add key="MyFeed" value="https://pkgs.dev.ops/Collection/_packaging/feedName/nuget/v3/index.json" />
      </packageSources>
    </configuration>
    
    1. Modify Dockerfile with the following instructions
      a. RUN command that downloads and execute the artifacts credentials provider shell script (Linux) during Docker build to install the nuget plugin
      b. copy nuget.config file to build stage
      c. set the VSS_NUGET_EXTERNAL_FEED_ENDPOINTS environment variable (replace feed_url with Azure DevOps feed URI and personal_access_token with Azure DevOps PAT)
      d. add -s feed_uri argument to dotnet restore command
    ...
    
    RUN curl -L https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | sh
    
    COPY **/*.csproj .
    COPY nuget.config .
    
    ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{\"endpointCredentials\": [{\"endpoint\":\"feed_url\", \"username\":\"docker\", \"password\":\"personal_access_token\"}]}"
    
    RUN dotnet restore "CustomProject.csproj" -s feed_uri
    
    ...