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

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

问题描述

我正在尝试使用本地 nuget 包进行 dotnet 还原,我尝试按照本教程进行操作:.

环境:

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

  • 来自 Visual Studio 2017 的基本 .Net Core Web 应用
  • Docker 企业版(无 UI),Windows 容器
  • Windows Server 2016 作为操作系统.

更新 10/15/2018

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

要准备好所有包,您需要在构建之前进行恢复.要在构建期间拥有所有包,您需要复制这些包.

这是一个实验形式的例子:

准备:

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

准备好 src/src.csproj:

<属性组><TargetFramework>netstandard2.0</TargetFramework></PropertyGroup><项目组><PackageReference Include="Newtonsoft.Json" Version="12.0.2"/></项目组></项目>

准备好 src/Dockerfile:

FROM microsoft/dotnet:2.2-sdk AS byse复制包/root/.nuget/packages复制源代码运行 ls/root/.nuget/packages工作目录/src运行 dotnet 还原运行 ls/root/.nuget/packages

执行:

恢复包:

docker run --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:复制包/root/.nuget/packages--->76c3e9869bb4步骤 3/7:复制 src src--->f0d3f8d9af0a步骤 4/7:运行 ls/root/.nuget/packages--->在 8323a9ba8cc6 中运行newtonsoft.json卸下中间容器 8323a9ba8cc6--->d90056004474步骤 5/7:工作目录/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成功标记测试:最新

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

步骤 4/7 在还原之前运行,并且包已经缓存.

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

这可以解决过多的恢复时间,例如在自动构建期间.

要解决您的网络问题,您可以尝试在解析步骤中安装网络补丁而不是本地路径,或者首先将 corp 网络中的 robocopy 文件安装到本地缓存中.

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天全站免登陆