如何使用多个基本映像构建Docker映像 [英] How to use multiple base images to build a docker image

查看:92
本文介绍了如何使用多个基本映像构建Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用基础docker映像(openjdk)创建了具有简单Java程序的映像。

I have used a base docker image (openjdk) to create an image having a simple Java Programme.

但是我想知道我是否在一个泊坞窗文件。我们该怎么做?
我不认为docker支持以下内容:

But I was wondering if I use 2 base images in a docker file. How can we do that? I don't think docker supports anything like:

FROM dockerImaage1
FROM dockerImage2

其中一种情况,为什么要这样做是我想要一张 ubuntu的图像具有 openjdk ,所以一种方法是我使用ubuntu的基础映像和d​​ocker文件中的write指令安装openjdk,设置JAVA_HOME变量等,

One of the scenarios, why this is required is I want an image of ubuntu having openjdk, so one way is I use the base image of ubuntu and the in docker file write instruction to install openjdk, set JAVA_HOME variable etc, which is undoubtedly cumbersome.

另一种方法,我喜欢在ubuntu的内部使用 openjdk 的基本图像(

Another alternative, I like is using the base image of openjdk inside base of ubuntu (if possible).

可能会有更典型的情况,我们可能需要的功能太糟糕了。

There may be more typical cases, where we may need is feature damn badly.

所以关于如何在docker文件中使用2个基本映像的任何想法?有没有人做过?

我找到了反向工程的链接此处,但是它有一些局限性,例如如果ubuntu的docker文件使用ADD或COPY之类的命令,则反向工程失败。

I found a link of reverse engineering here, but it has some limitations like if the docker file of ubuntu uses commands like ADD or COPY, the reverse engineering fails.

推荐答案

最新版本的docker具有多阶段构建的概念。请参阅:( https://docs.docker.com/engine/userguide / eng-image / multistage-build /

The latest version of docker has the concept of multi stage builds. Refer: (https://docs.docker.com/engine/userguide/eng-image/multistage-build/)

对于多阶段构建,您可以在Dockerfile中使用多个FROM语句。每个FROM指令可以使用不同的基数,并且每个都开始构建的新阶段。您可以有选择地将工件从一个阶段复制到另一个阶段,从而在最终图像中留下不需要的所有内容。

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

这篇关于如何使用多个基本映像构建Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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