复制 ..用于 ASP.NET 的 Dockerfile 中的命令 [英] COPY . . command in Dockerfile for ASP.NET

查看:22
本文介绍了复制 ..用于 ASP.NET 的 Dockerfile 中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

前 2 个 COPY 命令与 dotnet restore 以及完整的 COPYdotnet build<分开的原因/code> 是一个 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 container image builds. It is done this way so the project dependencies don't need to be installed every time a code change is made.

Docker 镜像是分层构建的.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 dependency installs are completed then you don't bust the cached dependency layer for every other change.

这是一个跨多种语言的通用主题,带有依赖项管理器.Go、Python、Node.js 等.Node.js 等效项在其余应用程序内容之前执行 package.jsonpackage-lock.json:

This is a common theme across many languages with a dependency manager. Go, Python, Node.js etc. The Node.js equivalent does the package.json and package-lock.json before the rest of the application contents:

WORKDIR /app
COPY package.json package-lock.json /app/
RUN npm install
COPY . /app/
CMD ["node", "app/index.js"]

这篇关于复制 ..用于 ASP.NET 的 Dockerfile 中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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