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:
2
Answers
I was able to resolve it by adding a Personal Access Token (PAT) as the password for the artifact source.
Example:
I also needed terminal to be running with Admin privileges.
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!