如何使用多个基础镜像构建一个 docker 镜像 [英] How to use multiple base images to build a docker image

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

问题描述

我使用了一个基础 docker 镜像 (openjdk) 来创建一个包含简单 Java 程序的镜像.

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

但我想知道我是否在 docker 文件中使用 2 个基本图像.我们怎么能做到这一点?我认为 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

其中一个场景,为什么需要这样做是我想要一个具有 openjdkubuntu 图像,所以一种方法是我使用 ubuntu 的基础图像和 indocker文件写入指令安装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天全站免登陆