10

I'm trying to download NuGet packages on Docker (Linux) for a .NET 6 application behind a corporate proxy

ARG netVersion=6.0

FROM mcr.microsoft.com/dotnet/sdk:${netVersion} AS build-env
WORKDIR /app

COPY company-root-ca.crt /usr/local/share/ca-certificates/company-root-ca.crt
RUN update-ca-certificates

COPY App/*.csproj .
RUN dotnet restore --configfile nuget.config

The dotnet restore call fails:

#17 [build-env 10/18] RUN dotnet restore --configfile nuget.config
#17 1.083   Determining projects to restore...
#17 6.883 /app/MyApp.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#17 6.900 /usr/share/dotnet/sdk/6.0.301/NuGet.targets(130,5): error : Sequence contains no elements [/app/MyApp.csproj]
#17 ERROR: executor failed running [/bin/sh -c dotnet restore --configfile nuget.config]: exit code: 1
------
 > [build-env 10/18] RUN dotnet restore --configfile nuget.config:
#17 1.083   Determining projects to restore...
#17 6.883 /app/MyApp.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#17 6.900 /usr/share/dotnet/sdk/6.0.301/NuGet.targets(130,5): error : Sequence contains no elements [/app/MyApp.csproj]

RUN curl https://api.nuget.org/v3/index.json in the container works fine, so the internet connection using our proxy is not the problem (it is set using build args). ENV DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER=0 doesn't seem to have any effect, like some moficiations in the nuget.config file which were suggested in different similiar questions/tickets:

<configuration>
<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy usesystemdefault="true" bypassonlocal="true" />
    </defaultProxy>
  <settings>
    <ipv6 enabled="true"/>
  </settings>
</system.net>

  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
<!--
  <activePackageSource>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" protocolVersion="3"  />
  </activePackageSource>
-->
</configuration>

Some references:

Daniel
  • 968
  • 3
  • 13
  • 32

2 Answers2

3

I was able to resolve it by adding a Personal Access Token (PAT) as the password for the artifact source.

Example:

# access token arg is passed in by build process                
ARG ACCESS_TOKEN="your PAT"
ARG ARTIFACTS_ENDPOINT="https://yoursource/v3/index.json"

# Configure the environment variables
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${ARTIFACTS_ENDPOINT}\", \"password\":\"${ACCESS_TOKEN}\"}]}"

I also needed terminal to be running with Admin privileges.

  • 1
    how to hide the PAT if i want to commit the dockerfile? – ulkas Jan 18 '23 at 07:41
  • You can leave it empty and pass it using `docker build --build-arg ACCESS_TOKEN` – 01F0 Jan 26 '23 at 08:28
  • Are you using this? https://github.com/microsoft/artifacts-credprovider because in some articles it's said that new .net versions can just use without the cred provider. I am having the problem with or without cred provider. I am also in .Net 6. Can you please post your docker file? – user2058413 Mar 03 '23 at 08:24
  • The format you have provided is not correct. ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{`"endpointCredentials`": [{`"endpoint`":`"https://deepak.com/nuget/v3/index.json`", `"username`":`"hee@asdf.com`", `"password`":`"${FEED_PAT_TOKEN}`"}]}" – Deepak paramesh Apr 19 '23 at 11:52
  • @Deepakparamesh What is the `username` here ? how to deal with it inside a pipeline as we can't put usernames there. – Venkata Dorisala Jun 06 '23 at 09:48
  • username is your / company mail id @VenkataDorisala – Deepak paramesh Jun 08 '23 at 07:27
-5

This often happens when there are multiple networking adapters (Ethernet, Wi-Fi, etc.) present on the host. The priority of these adapters needs to be configured properly in order for the Windows networking stack to correctly choose gateway routes. You can fix this by setting your primary internet-connected networking adapter to have the lowest InterfaceMetric value. Try these Powershell commands from an elevated console:

Get-NetIPInterface -AddressFamily IPv4 | Sort-Object -Property InterfaceMetric -Descending

Again, you want your host's primary internet-connected network adapter to have the lowest InterfaceMetric value.

Use this command to make the change (example assumes primary adapter InterfaceAlias is 'Wi-Fi'):

Set-NetIPInterface -InterfaceAlias 'Wi-Fi' -InterfaceMetric 3

That should do it. If your host's primary network adapter is bridged because you have an External virtual switch setup in Hyper-V, then you will set the external virtual switch to have the lowest InterfaceMetric value.

You can verify your routing tables by using this command (the last line should show the primary adapter's gateway address along with it's ifMetric value):

Get-NetRoute -AddressFamily IPv4

I hope this helps!