如何使用本地nuget软件包源进行Dockerfile dotnet还原 [英] How to use local nuget package sources for Dockerfile dotnet restore

查看:486
本文介绍了如何使用本地nuget软件包源进行Dockerfile dotnet还原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本地nuget软件包进行dotnet还原,我尝试遵循此教程:。



环境:



对于示例项目,我使用了:




  • Visual Studio 2017中的基本.Net Core Web应用

  • Docker企业版(无UI),Windows容器

  • Windows Server 2016作为操作系统。



更新10/15/2018



虽然@omajid的回答非常有帮助,但我相信只有在使用docker run时才可能进行docker volume挂载,并且不能在Dockerfile中使用(用于构建管道)。得到了与我要实现的链接相似的链接。 如何在Docker容器中装载主机目录

解决方案

要准备好所有软件包,需要在构建之前进行还原。
要在构建期间拥有所有软件包,您需要复制这些软件包。



以下是一个实验形式的示例:



准备:



准备好SDK: docker pull microsoft / dotnet:2.2-sdk



已准备好 src / src.csproj

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

已准备好 src / Dockerfile

 从Microsoft / dotnet:2.2-sdk AS byse 
COPY包/root/.nuget/packages
COPY src src
RUN ls /root/.nuget/packages
WORKDIR / src
RUN dotnet restore
RUN ls /root/.nuget/packages

执行:



还原软件包:

  docker运行--rm -v $ {pwd)/ src:/ src -v $ {pwd)/packages:/root/.nuget/packages -w / src microsoft / dotnet:2.2-sdk dotnet恢复

构建映像:

  docker build -t test -f src / Dockerfile。 

期望:

 将构建上下文发送到Docker守护程序13.77MB 
步骤1/7:从microsoft / dotnet:2.2-sdk AS byse
---> e4747ec2aaff
步骤2/7:COPY包/root/.nuget/packages
---> 76c3e9869bb4
步骤3/7:复制src src
---> f0d3f8d9af0a
步骤4/7:运行ls /root/.nuget/packages
--->在8323a9ba8cc6中运行
newtonsoft.json
删除中间容器8323a9ba8cc6
---> d90056004474
步骤5/7:WORKDIR / src
--->在f879d52f81a7
中运行除去中间容器f879d52f81a7
---> 4020c789c338
步骤6/7:运行dotnet restore
--->在ab62a031ce8a中运行
/src/src.csproj的还原已在44.28毫秒内完成。
移除中间容器ab62a031ce8a
---> 2cd0c01fc25d
步骤7/7:运行ls /root/.nuget/packages
--->在1ab3310e2f4c中运行
newtonsoft.json
删除中间容器1ab3310e2f4c
---> 977e59f0eb10
成功构建了977e59f0eb10
成功标记了test:latest

请注意,ls步骤已缓存,并且不会在后续调用中打印。运行 docker rmi test 进行重置。



在还原之前,将运行步骤4/7,并且已经缓存了程序包。

 步骤4/7:运行ls /root/.nuget/packages 
--->在8323a9ba8cc6中运行
newtonsoft.json

这可以解决过多的还原时间,例如在自动构建期间



要解决您的网络问题,您可以在解决步骤中尝试安装网络补丁而不是本地路径,或者先将公司网络中的文件自动复制到本地缓存中


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..

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

解决方案

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.

这篇关于如何使用本地nuget软件包源进行Dockerfile dotnet还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆