Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

dotnet restore fails with NU1301 inside docker build

Errorerror NU1301: Unable to load the service index for source

Short answer

NU1301 means restore could not use one of the configured package sources. In a docker build the usual reasons are a source in nuget.config that does not exist in the container, a proxy the build does not know about, or DNS that fails inside the container. Read the line after NU1301 to see which source failed, then fix that one.

  • Applies to .NET SDK 8, .NET SDK 9, Docker Engine 28, Docker Buildx 0.2x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the restore step alone with detailed output so the failing source URL or path is printed, then compare it to the sources in your nuget.config.

    dotnet restore --verbosity detailed
    cat nuget.config
  2. 2Match what you see, then jump to its fix

    1. 1NU1301 names a local path such as C:\... or a feed on a private network, and the message says the source does not exist or cannot be reached.A source in nuget.config does not exist inside the containerFix
    2. 2The failing source is a normal https address such as api.nuget.org, and the same restore works on a machine outside the proxy.The build sits behind a proxy and restore does not use itFix
    3. 3The host can open the feed URL in a browser, but the container cannot resolve its name.Name lookup fails inside the containerFix

What does the error look like?

  • error NU1301: Unable to load the service index for source
  • NU1301: The local source 'C:\Code\Contoso\contosoLocalSource' doesn't exist.

Cause 1 of 3

A source in nuget.config does not exist inside the container

1Check

A nuget.config copied into the build context is read by restore in the container. A Windows folder path or a private feed address that only works on your machine fails there.

cat nuget.config

2Fix

Microsoft's guidance for NU1301 is to remove a source the build does not need. Put a clear element first so machine-specific sources are ignored, then list only the sources the container can reach.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

3Verify

Rebuild without the cache so the restore step really runs again.

docker build --no-cache --progress=plain -t my-app .

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The build sits behind a proxy and restore does not use it

1Check

NuGet reads http_proxy and no_proxy from nuget.config or from environment variables, and a build container does not receive your shell's variables by itself.

env | grep -i proxy

2Fix

Pass the proxy values to the build as build arguments, which Docker accepts as predefined build arguments without an ARG line, or set http_proxy in the nuget.config used by the build.

docker build \
  --build-arg HTTP_PROXY=http://proxy.example.com:3128 \
  --build-arg HTTPS_PROXY=http://proxy.example.com:3128 \
  --build-arg NO_PROXY=localhost,127.0.0.1 \
  -t my-app .

3Verify

The restore step finishes and the build moves on to compile.

docker build --progress=plain -t my-app .

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

Name lookup fails inside the container

1Check

Containers use the host's DNS servers by default. A resolver on the host's loopback address, common with local DNS caches or VPN clients, does not work from inside a container, because 127.0.0.1 there is the container itself.

cat /etc/resolv.conf
docker run --rm mcr.microsoft.com/dotnet/sdk:9.0 getent hosts api.nuget.org

2Fix

Give the daemon a DNS server the container can reach, or pass one to a single container with --dns to confirm the cause.

docker run --rm --dns 1.1.1.1 mcr.microsoft.com/dotnet/sdk:9.0 getent hosts api.nuget.org

3Verify

The lookup returns an address, then the image builds.

docker build --progress=plain -t my-app .

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Commit a nuget.config with a clear element and only the sources a clean machine can reach.
  • Restore in its own layer after copying only the project files, so a network failure shows up at the restore line.
  • Keep private feed credentials out of the image; pass them as build secrets.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.