问题描述
用于Docker的Visual Studio工具为包含 COPY的ASP.NET项目创建一个Dockerfile。 。
命令如下:
The Visual Studio tooling for Docker creates a Dockerfile for ASP.NET projects containing a COPY . .
command as below:
WORKDIR /src
COPY *.sln ./
...
COPY . .
根据我的阅读,< src>
参数是相对于上下文的,因此不受 WORKDIR / src
命令的影响。但是< dest>
是相对于 WORKDIR
的,因此将指向 / src
。
From what I've read, the <src>
parameter is relative to the context, so isn't affected by the WORKDIR /src
command. The <dest>
however is relative to the WORKDIR
so will be pointing at /src
.
此命令是否只是从根目录移出其余文件进行打包(docker-compose.yml,.dockerignore等)?如果是这样,那么为什么要在 RUN dotnet build ...
命令之前完成?
Is this command just bringing over the remaining files from the root for packaging (docker-compose.yml, .dockerignore, etc.)? If so, then why is this done ahead of the RUN dotnet build...
command?
下面的完整Dockerfile :
Full Dockerfile below:
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY MyProject/MyProject.csproj MyProject/
RUN dotnet restore
COPY . . # The line mentioned above
WORKDIR /src/MyProject
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]
推荐答案
COPY。 。
The COPY . .
copies the entire project, recursively into the container for the build.
前两个 COPY 命令,其中 dotnet
,然后是完整的
restore COPY
,其中 dotnet build
是一个Docker缓存技巧,可加快构建速度。这样可以避免每次更改代码时都需要安装项目依赖项。
The reason for the separation of the first 2 COPY
commands with dotnet restore
and then the complete COPY
with dotnet build
is a Docker caching trick to speed up builds. It is done this way so the project dependencies don't need to be installed every time a code change is made.
。 Docker将构成每个新层的内容和说明与以前的版本进行比较。如果它们与现有层的SHA256校验和匹配,则可以跳过该层的构建步骤。
Docker images are built in layers. Docker compares the contents and instructions that would make up the each new layer to previous builds. If they match the SHA256 checksum for the existing layer, the build step for that layer can be skipped.
代码的变化远比依赖项大,现在通常从慢速(ish)网络中获取依赖项。如果您在相关性完成后复制代码,则不会破坏其他所有更改的缓存的相关性层。
Code changes a lot more than dependencies, and dependencies are usually fetched from a slow(ish) network now. If you copy the code after the dependencies are completed then you don't bust the cached dependency layer for every other change.
等效于Node.js的应用在内容前执行 package.json
:
The Node.js equivalent does the package.json
before the app contents:
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY . /app/
CMD ["node", "app/index.js"]
这篇关于复制。 。 Dockerfile中用于ASP.NET的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!