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

查看:133
本文介绍了如何在 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?

这种方法的问题是解决方案中的所有项目文件都需要单独复制,如果项目真的很大,并且不时添加和删除项目,保持 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.

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

Importantly, the ADD and COPY instructions get special treatment:

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

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