Or: Nuget Restore fails on docker build (that was my case, yay!)
Or: The command ‘cmd /S /C dotnet restore returned a non-zero code: 1 in Docker build

First of all, i found a temporary solution on Stackoverflow

Try this

docker network ls

This should list the networkIDs try running the docker build

docker build --network=<networkid> tag=<tag>

try the above with all the network listed it will work with one, that’s the host network.

(In my case, i used the ID of the NAT Network

Anyhow, temporary solutions are temporary, so i kept looking for something better.

And according to this article on CodeBucket the reason for this problem is that the docker DNS is broken, and it’s possible to set the DNS server in the docker configuration.

You’ll need to open /etc/docker/daemon.json on LINUX machines and alternatively open C:\ProgramData\docker\config\daemon.json on Windows machines.
When using Docker for Windows, you can alternatively just use the Docker for Windows UI by right clicking on the Docker Icon in the Taskbar, opening settings and navigating to “Docker Engine”

Then you’ll need to hard-config your DNS Servers by adding this line into the JSON:

“dns”: [“10.1.2.3”, “8.8.8.8”]

A fresh install with this line added will look something like this

{
“experimental”: false,
“dns”: [“10.1.2.3”, “8.8.8.8”]
}

Et viola, the problem is fixed.
Next time your build should work “out of the box”