如何在Dockerfile中正确使用dotnet restore [英] How to use dotnet restore properly in Dockerfile

查看:246
本文介绍了如何在Dockerfile中正确使用dotnet restore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将docker支持添加到ASP.NET Core项目中时,VS(15.9.2)将添加一个默认的Dockerfile来进行还原,构建和发布.但是,不仅将所有文件复制到Docker构建容器中,它还仅复制proj文件,进行还原,然后在构建之前复制其余文件.我想知道为什么要这样做吗?与直接复制所有文件然后进行还原有何不同?

When adding docker support to a ASP.NET Core project VS (15.9.2) will add a default Dockerfile that does restore, build and publish. But instead of just copying all files into the Docker build container it first copies just the proj-files, does the restore and then copies the rest before it builds. I was wondering why this is done like this? In what way is that different from just copying all files directly and then doing the restore?

这种方法的问题是解决方案中的所有proj文件都需要分别复制,并且如果项目真的很大,并且不时添加和删除项目,则很难保持Dockerfile同步有了这个.我只想知道为什么要这样做,而复制所有内容是否也一样?

The problem with this approach is that all proj-files in the solution will need to be copied separately and if the project is really big with projects being added and removed from time to time it's a bit hard to keep the Dockerfile in synch with this. I just like to know why this is done like this and if it would be just as ok to copy everything instead?

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Temp2/Temp2.csproj", "Temp2/"]
COPY ["Temp3/Temp3.csproj", "Temp3/"]
RUN dotnet restore "Temp2/Temp2.csproj"
COPY . .
WORKDIR "/src/Temp2"
RUN dotnet build "Temp2.csproj" -c Release -o /app

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY . .
RUN dotnet restore "Temp2/Temp2.csproj"
WORKDIR "/src/Temp2"
RUN dotnet build "Temp2.csproj" -c Release -o /app

推荐答案

当Docker构建映像时,它会维护

When Docker builds an image, it maintains a build cache:

构建映像时,Docker逐步执行Dockerfile中的指令,并按指定的顺序执行每个指令.在检查每条指令时,Docker会在其缓存中寻找一个可以重用的现有映像,而不是创建一个新的(重复的)映像.

When building an image, Docker steps through the instructions in your Dockerfile, executing each in the order specified. As each instruction is examined, Docker looks for an existing image in its cache that it can reuse, rather than creating a new (duplicate) image.

重要的是, ADD COPY 指令得到特殊处理:

Importantly, the ADD and COPY instructions get special treatment:

对于 ADD COPY 指令,将检查图像中文件的内容,并为每个文件计算一个校验和.在这些校验和中不考虑文件的最后修改时间和最后访问时间.在缓存查找期间,将校验和与现有映像中的校验和进行比较.如果文件中的任何内容(例如内容和元数据)发生了更改,则缓存将无效.

For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.

在构建.NET Core解决方案时,我们可以确保在运行 dotnet restore 之后,再次运行 dotnet restore 的结果仅在.csproj 文件已更改(例如,添加了新软件包或更改了版本).

When building a .NET Core solution, we can be sure that after running dotnet restore, the result of running dotnet restore again will only change if the .csproj file has changed (e.g. a new package is added or a version is changed).

通过将 .csproj 文件分别复制到映像 中,我们可以利用Docker的构建缓存,这意味着只要 .csproj 文件未更改,每次重建映像时,都不会不必要地重新执行 dotnet restore 步骤.

By copying the .csproj files into the image separately, we can take advantage of Docker's build cache, which means that so long as the .csproj file hasn't changed, the dotnet restore step will not be re-executed unnecessarily each and every time the image gets rebuilt.

这篇关于如何在Dockerfile中正确使用dotnet restore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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