Docker多阶段构建而不复制以前的映像? [英] Docker multistage build without copying from previous image?

查看:163
本文介绍了Docker多阶段构建而不复制以前的映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您不从先前构建的映像中复制任何文件,在Docker中使用多阶段构建是否有任何优势?
例如。

 从some_base_image作为基础
#一些随机命令
RUN mkdir / app
RUN mkdir / app2
RUN mkdir / app3
#ETC

#第二阶段从第一阶段开始
从基础

#将一些文件添加到图像
复制foo.txt / app

此结果是否导致与非多阶段版本相比,图像较小还是具有其他优势?还是多阶段构建仅对准备一些文件然后将其复制到另一个基本映像有用?

解决方案


或者多阶段构建仅对准备一些文件然后将其复制到另一个基本映像有用吗?


这是主要用途 使用多阶段构建中讨论的案例p>

主要目标是通过将文件从一个映像复制到另一个映像来减少层数,而不包括生成所述文件所需的构建环境。



但是,另一个目标可能是重建整个Dockerfile,包括每个阶段。



然后您的建议(不复制)仍然适用。


您可以指定目标构建阶段。以下命令假定您使用的是先前的Dockerfile,但在名为builder的阶段停止:

  $ docker build --target builder- t alexellis2 / href-counter:latest。 

一些可能非常强大的方案是:




  • 调试特定的构建阶段

  • 使用带有所有调试符号的 debug 阶段或启用了工具,并实现了精益的生产阶段

  • 使用 testing 阶段您的应用会填充测试数据,但要使用其他使用真实数据的阶段构建产量



does it have any advantages to use a multistage build in Docker, if you don't copy any files from the previously built image? eg.

FROM some_base_image as base
#Some random commands
RUN mkdir /app
RUN mkdir /app2
RUN mkdir /app3
#ETC

#Second stage starts from first stage
FROM base

#Add some files to image
COPY  foo.txt /app

Does this result in a smaller image or offer any other advantages compared to a non multi-stage version? Or are multi stage builds only useful for preparing some files and then copying those into another base image?

解决方案

Or are multi stage builds only useful for preparing some files and then copying those into another base image?

This is the main use-case discussed in "Use multi-stage builds"

The main goal is to reduce the number of layers by copying files from one image to another, without including the build environment needed to produce said files.

But, another goal could be not rebuild the entire Dockerfile including every stage.

Then your suggestion (not copying) could still apply.

You can specify a target build stage. The following command assumes you are using the previous Dockerfile but stops at the stage named builder:

$ docker build --target builder -t alexellis2/href-counter:latest .

A few scenarios where this might be very powerful are:

  • Debugging a specific build stage
  • Using a debug stage with all debugging symbols or tools enabled, and a lean production stage
  • Using a testing stage in which your app gets populated with test data, but building for production using a different stage which uses real data

这篇关于Docker多阶段构建而不复制以前的映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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