7

I'm trying to make use of local nuget package for my dotnet restore, I tried to follow this tutorial: dotnet restore w/out internet

My problem:

It doesn't see the path even though it exist on that path.click for image.

The server I'm using is on a Corporate Network that is why I can't use dotnet restore, so I'm also experiencing the problem with nuget.org similar to this link.

Environment:

For the sample project, I used:

  • the basic .Net Core web app from Visual Studio 2017
  • Docker Enterprise Edition(no UI), Windows container
  • Windows Server 2016 as OS.

UPDATE 10/15/2018

While the answer of @omajid has been very helpful, I believe docker volume mount is only possible when using docker run and can't be used in Dockerfile(which will be used for Build Pipeline). Got this link which is similar to what I want to achieve. How to mount a host directory in a Docker container

jravenger4
  • 143
  • 1
  • 2
  • 10

4 Answers4

4

To have all packages ready you need restore before building. To have all packages during the build you need to copy the packages.

Here is an example in form of an experiment:

Preparation:

Have the sdk ready: docker pull microsoft/dotnet:2.2-sdk.

Have src/src.csproj ready:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>
</Project>

Have src/Dockerfile ready:

FROM microsoft/dotnet:2.2-sdk AS byse
COPY packages /root/.nuget/packages
COPY src src
RUN ls /root/.nuget/packages
WORKDIR /src
RUN dotnet restore
RUN ls /root/.nuget/packages

Execution:

Restore the Packages:

docker run --rm -v $(pwd)/src:/src -v $(pwd)/packages:/root/.nuget/packages -w /src  microsoft/dotnet:2.2-sdk dotnet restore

Build the Image:

docker build -t test -f src/Dockerfile .

Expectation:

Sending build context to Docker daemon  13.77MB
Step 1/7 : FROM microsoft/dotnet:2.2-sdk AS byse
 ---> e4747ec2aaff
Step 2/7 : COPY packages /root/.nuget/packages
 ---> 76c3e9869bb4
Step 3/7 : COPY src src
 ---> f0d3f8d9af0a
Step 4/7 : RUN ls /root/.nuget/packages
 ---> Running in 8323a9ba8cc6
newtonsoft.json
Removing intermediate container 8323a9ba8cc6
 ---> d90056004474
Step 5/7 : WORKDIR /src
 ---> Running in f879d52f81a7
Removing intermediate container f879d52f81a7
 ---> 4020c789c338
Step 6/7 : RUN dotnet restore
 ---> Running in ab62a031ce8a
  Restore completed in 44.28 ms for /src/src.csproj.
Removing intermediate container ab62a031ce8a
 ---> 2cd0c01fc25d
Step 7/7 : RUN ls /root/.nuget/packages
 ---> Running in 1ab3310e2f4c
newtonsoft.json
Removing intermediate container 1ab3310e2f4c
 ---> 977e59f0eb10
Successfully built 977e59f0eb10
Successfully tagged test:latest

Note that the ls steps are cached and would not print on a subsequent call. Run docker rmi test to reset.

Step 4/7 runs before the restore and the packages are already cached.

Step 4/7 : RUN ls /root/.nuget/packages
 ---> Running in 8323a9ba8cc6
newtonsoft.json

This can solves excessive restore times for example during automated builds.

To solve your network issue you can try to mount the network patch instead of the local path during the resolve step or robocopy files from your corp network into a local cache first.

Johannes
  • 6,490
  • 10
  • 59
  • 108
1

The entire selling point of docker and container technologies is isolation. So within a docker container, your user disk is not visible. If it was, there would be much less of an isolation. You need to mount your local nuget directory inside the container to be able to access it. For detailed steps, see https://rominirani.com/docker-on-windows-mounting-host-directories-d96f3f056a2c.

In particular:

  • Share your C: drive
  • Within your Dockerfile, have a dotnet restore --source /packages
  • Use a volume mount to mount your local packages into /packages inside the container: docker build -t webapp4 . -v c:/users/cnaling/.nuget/packages:/packages
omajid
  • 14,165
  • 4
  • 47
  • 64
  • Thank you for the answer. Just wondering if the share drive is also possible for Docker EE(no UI). I'm trying your suggestion now and also googling about docker command for shared drive. – jravenger4 Oct 12 '18 at 04:54
  • I don't use docker on Windows. I can't say the exact steps, but double check if sharing the C: drive is needed explicitly. Googling says some recent versions of Docker EE + Windows 2016 don't require that step. – omajid Oct 12 '18 at 13:50
1

One simpler alternative is to cache the NuGet packages in a separate layer by having them being restored from the csproj project file before copying the source files. Example:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build
COPY . ./

RUN dotnet publish -c Release -o out

This solution should speed up local development which typically requires quite a few stop -> build -> up cycles.

The main downside is that changing csproj triggers the layer being rebuilt, but this should happen less frequently than rebuilding of the container.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

I am a little late to the game but I believe I found a simple solution to this problem...

  1. Create a "NuGet.Config" file in the same directory as your .sln
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="{{CUSTOM NAME}}" value="{{CUSTOM SOURCE}}" />
  </packageSources>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <bindingRedirects>
    <add key="skip" value="False" />
  </bindingRedirects>
  <packageManagement>
    <add key="format" value="0" />
    <add key="disabled" value="False" />
  </packageManagement>
</configuration>
  1. That is it! Create your "Dockerfile" here as well

  2. Run docker build with your Dockerfile and all will get resolved

Edd
  • 703
  • 7
  • 23