如何使用本地 nuget 包源进行 Dockerfile dotnet restore

编程入门 行业动态 更新时间:2024-10-24 11:15:36
本文介绍了如何使用本地 nuget 包源进行 Dockerfile dotnet restore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

环境:

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

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

虽然@omajid 的回答非常有帮助,但我相信 docker 卷挂载只有在使用 docker run 时才有可能,并且不能在 Dockerfile 中使用(它将用于 Build Pipeline).得到了这个链接,这与我想要实现的相似.如何在 Docker 容器中挂载主机目录

解决方案

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

下面是一个实验形式的例子:

准备工作:

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

准备好src/src.csproj:

<Project Sdk="Microsoft.NET.Sdk"><属性组><TargetFramework>netstandard2.0</TargetFramework></属性组><项目组><PackageReference 包含="Newtonsoft.Json" 版本="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--->运行在 8323a9ba8cc6newtonsoft.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--->运行在 8323a9ba8cc6newtonsoft.json

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

要解决您的网络问题,您可以尝试在解析步骤中安装网络补丁而不是本地路径,或者首先将您公司网络中的文件 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 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 restore

本文发布于:2023-11-14 19:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   nuget   Dockerfile   dotnet   restore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!