I use multi stage Dockerfile with stages build
and start
, as in:
我将多阶段Dockerfile与阶段Build和Start一起使用,如下所示:
FROM <whatever> as build
WORKDIR /app
COPY ./a.file ./
COPY ./b.file ./
RUN <build command>
FROM build as start
WORKDIR /app
COPY --from=build /app/a.file ./
COPY --from=build /app/b.file ./
CMD <start command>
I wonder, in start
stage, do I have to do COPY --from=build <...files>
again?
我想知道,在Start阶段,我是否需要再次执行Copy--from=Build<...
In other words, does start
reuse file system of build
? If it does, then I don't need to copy the files again (since all files necessary for start
have already been copied in build
). Is that correct?
换句话说,是不是开始重用文件系统的构建?如果是这样,那么我就不需要再次复制文件(因为启动所需的所有文件都已经在构建中复制了)。对吗?
更多回答
Why do you have a second stage?
为什么你会有第二阶段?
@BMitch The build
stage is cached if its inputs haven't changed (I do have some difference in file content between the stages, – i.e., some files are copied in start
but not in build
)
@BMitch如果构建阶段的输入没有更改(我确实在两个阶段的文件内容上有一些不同,即某些文件在Start中被复制,但在Build中不被复制),构建阶段将被缓存
The build cache is per step, not per stage. As Hans answered, the second FROM
is not needed unless you build the first stage only, or reuse the second stage multiple times elsewhere in your file.
构建缓存是按步骤而不是按阶段进行的。正如Hans回答的那样,第二个FROM是不需要的,除非你只构建第一个阶段,或者在文件的其他地方多次重用第二个阶段。
优秀答案推荐
No you don't. In your Dockerfile, the second FROM
statement doesn't do anything at all, since the start
stage continues on from the build
stage. So you can remove the second FROM
statement and your final image will be the same as before.
不,您不需要。在您的Dockerfile中,第二个from语句根本不做任何事情,因为开始阶段从构建阶段继续。因此,您可以删除第二个From语句,您的最终图像将与之前相同。
The reason people use multi-stage Dockerfiles is usually to build the app in the first stage with an image that contains a build environment. Then, after the build is done, you start a new run-time image and copy over the built application.
人们使用多阶段Dockerfile的原因通常是在第一阶段使用包含构建环境的镜像构建应用程序。然后,在构建完成后,启动一个新的运行时映像并复制构建的应用程序。
That way your final image doesn't contain the complete build environment and you create a smaller final image.
这样,您的最终映像不包含完整的构建环境,您可以创建一个较小的最终映像。
更多回答
If I understand correctly, you describe a Dockerfile in which the start stage does not use build stage, but the "empty" one (e.g., FROM node AS build
, and then FROM node AS start
), right?
如果我理解正确的话,您描述了一个Dockerfile,其中的开始阶段不使用Build阶段,而是“空的”阶段(例如,从节点作为Build,然后从节点作为Start),对吗?
我是一名优秀的程序员,十分优秀!