title: Dotnet restore from Docker over VPN
body: <p>A build in a Docker container fails on dotnet restore, erroring on retrieving nuget packages from a private repo</p>
  
  <p><code>
  FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
  ...
  RUN dotnet restore
    Retrying 'FindPackagesByIdAsyncCore' for source 'http://privaterepo.somedomain.com/FindPackagesById()?id='somepackagename'&amp;semVerLevel=2.0.0'.
    Connection refused
  </code></p>
  
  <p>Ultimately the restore command fails when it can't retrieve the necessary packages</p>
  
  <p>These packages on a private repo need to be accessed via VPN.  Building the solution works fine in Visual Studio but the build in the container always fails.  This may be happening because:</p>
  
  <ol>
  <li>VS has retry logic for retrieving the packages on restore, the CL in a Docker container may not be as forgiving</li>
  <li>May not be able to access repo from within the container due to the way the network is configured</li>
  </ol>
  
  <p>My workaround is to copy the relevant locally cached packages (from C:\Users\username.nuget\packages ) to a packages folder on the root of the project and then add this line to the Dockerfile above the restore command:</p>
  
  <p><code>COPY packages /root/.nuget/packages</code></p>
  
  <p>This workaround allows the restore command to complete successfully but slows down the build substantially and puts potentially unecessary files in the build folder.  Is there a better solution?</p>
  
  <p>You could run a <a href="https://dotnetthoughts.net/running-a-nuget-server-on-docker/" rel="nofollow noreferrer">NuGet server in a Docker container</a> but that's also overhead that is probably unecessary.</p>