了解多阶段DockerFile构建 [英] Understanding multi stage DockerFile builds

查看:53
本文介绍了了解多阶段DockerFile构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手.我的项目具有以下目录结构

I am new to Docker. I have the following directory structure for my project

docker-compose.yml
|-services
  |-orders
    |-DockerFile

我正在使用具有以下内容的标准ASP.Net Core DockerFile:

I am using standard ASP.Net Core DockerFile that has the following content:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["Services/Orders/Orders.csproj", "Services/Orders/"]
RUN dotnet restore "Services/Orders/Orders.csproj"
COPY . .
WORKDIR "/src/Services/Orders"
RUN dotnet build "Orders.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Orders.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Orders.dll"]

我的docker-compose.yml文件具有

My docker-compose.yml file has

# Please refer https://aka.ms/HTTPSinContainer on how to setup an https developer certificate for your ASP .NET Core service.
version: "3.4"

services:
  orders-api:
    image: orders-api
    build:
      context: .
      dockerfile: Services/Orders/Dockerfile
    ports:
      - "2222:80"

我对这两个文件有些困惑

I have some confusion with these two files

  • 问题1:第2行的WORKDIR/app有什么用途?

我的理解是我们正在使用可以扩展的基本图像当我们在第1行中导入基本图像,然后设置第2和3行中的WORKDIR和端口,将被以下命令使用使用这张图片?

My understanding is that we are using a base image that we can extend so when we import the base image in line number 1 and then set the WORKDIR and port in line number 2 and 3, will they be used by following commands that use this image?

问题2:为什么我们为SDK映像设置 WORKDIR/src ,而不是为 WORKDIR/app ?

问题3:复制命令中的路径与Dockerfile或Docker-compose.yml文件相关吗?

在COPY ["Services/Orders/Orders.csproj","Services/Orders/"]行中,我们指定的路径似乎与docker-compose.yml文件相关,而与DockerFile进一步嵌套在文件夹中.这是否意味着Dockerfile中的路径需要与docker-compose.yml相关?我之所以这样问,是因为我想如果我使用此Dockerfile运行docker build命令,则会由于复制命令中的路径无效而收到错误消息.

In the line COPY ["Services/Orders/Orders.csproj", "Services/Orders/"], the path that we are specifying seems to be relevant to the docker-compose.yml file and not the DockerFile which is nested down further in folders. Does this mean that paths in Dockerfile need to be relevant to docker-compose.yml? I am asking this because I am thinking that if I run the docker build command using this Dockerfile then I will get an error since the path in the copy command will not be valid.

推荐答案

问题1:第2行上WORKDIR/app的用途是什么?

这定义了您当前的工作目录.默认情况下,将从该位置运行以下命令,并且相对路径将从该位置开始.

This defines your current working directory. Following commands will by default be run from that location and relative paths will start there.

具有以下文件结构的示例:

Example with the following file structure:

/app/
  - README.md
  - folder1/
       - some.txt

WORKDIR /app
RUN ls

将打印

folder1
README.md

WORKDIR /app/folder1
RUN ls

将打印

some.txt

问题2:为什么要为SDK映像设置WORKDIR/src而不是为WORKDIR/app设置?

请参阅问题1的答案.在/src 源目录中执行以下 COPY dotnet restore 命令.(构建是从/src 目录完成的,以后在/app/publish 中创建的工件会复制到最后一个阶段,将在该/app 目录中执行.)

See the answer to Question 1. The following COPY and dotnet restore command are executed in the /src source directory. (The build is done from the /src directory and the created artifact in /app/publish is later copied to the /app directory in the last stage, to be executed from that /app directory.)

问题3:复制命令中的路径与Dockerfile或Docker-compose.yml文件相关吗?

复制采用两条路径.第一个引用源(来自构建Docker镜像的上下文中的文件或文件夹),第二个引用目标(生成的Docker镜像中的文件顺序文件夹).因此,这些路径通常仅特定于Dockerfile并且独立于您的docker-compose项目.

Copy takes two paths. The first one references the source (a file or folder from the context the docker image is build from) and the second one references the destination (a file order folder in your resulting docker image.) Hence these paths are usually only specific to the Dockerfile and independent from your docker-compose project.

但是,在您的情况下,docker镜像构建的上下文是在docker-compose.yml中定义的:

However in your case the context of your docker image build is defined in the docker-compose.yml here:

build:
      context: .
      dockerfile: Services/Orders/Dockerfile

,因此您的docker映像映像构建的上下文似乎是docker-compose.yml所在的目录.但是,如果仅在该文件夹中运行 docker build -f Services/Orders/Dockerfile.,则可以构建相同的映像.(并非特定于docker-compose)

and therefore the context of your docker image image build seems to be the directory where your docker-compose.yml is located. You could build the same image though if you just run docker build -f Services/Orders/Dockerfile . in that folder. (It is not docker-compose specific)

因此,您应该从docker-compose.yml所在的目录开始,在 ./Services/Orders/中找到 Orders.csproj .此文件已被复制在第二个构建阶段中/src/Services/Orders/Orders.csproj .(可以在 COPY 语句中提交/src ,因为它是从当前工作目录开始的相对路径,该目录在上一行中定义.-请参阅问题1)

Therefore you should find Orders.csproj in ./Services/Orders/ starting from the directory your docker-compose.yml is located in. This file is than copied to /src/Services/Orders/Orders.csproj in your second build stage. (The /src can be commited in the COPY statement as it is a relative path starting from your current working directory, which is defined in the line above. - See Question 1)

这篇关于了解多阶段DockerFile构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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