有没有办法将 Docker 镜像合并到 1 个容器中? [英] Is there a way to combine Docker images into 1 container?

查看:57
本文介绍了有没有办法将 Docker 镜像合并到 1 个容器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有几个 Dockerfile.

I have a few Dockerfiles right now.

一个是针对Cassandra 3.5的,它是FROM cassandra:3.5

One is for Cassandra 3.5, and it is FROM cassandra:3.5

我也有一个用于 Kafka 的 Dockerfile,但它要复杂得多.它是 FROM java:openjdk-8-fre 并且它运行一个长命令来安装 Kafka 和 Zookeeper.

I also have a Dockerfile for Kafka, but t is quite a bit more complex. It is FROM java:openjdk-8-fre and it runs a long command to install Kafka and Zookeeper.

最后,我有一个用 Scala 编写的使用 SBT 的应用程序.

Finally, I have an application written in Scala that uses SBT.

对于那个 Dockerfile,它是 FROM Broadinstitute/scala-baseimage,它为我提供了 Java 8、Scala 2.11.7 和 STB 0.13.9,这正是我所需要的.

For that Dockerfile, it is FROM broadinstitute/scala-baseimage, which gets me Java 8, Scala 2.11.7, and STB 0.13.9, which are what I need.

也许,我不了解 Docker 的工作原理,但我的 Scala 程序将 Cassandra 和 Kafka 作为依赖项,出于开发目的,我希望其他人能够使用 Dockerfile 简单地克隆我的存储库然后能够使用 Cassandra、Kafka、Scala、Java 和 SBT 构建它,所有这些都内置,以便他们可以编译源代码.不过,我对此有很多问题.

Perhaps, I don't understand how Docker works, but my Scala program has Cassandra and Kafka as dependencies and for development purposes, I want others to be able to simply clone my repo with the Dockerfile and then be able to build it with Cassandra, Kafka, Scala, Java and SBT all baked in so that they can just compile the source. I'm having a lot of issues with this though.

如何组合这些 Dockerfile?我如何简单地创建一个包含这些东西的环境?

How do I combine these Dockerfiles? How do I simply make an environment with those things baked in?

推荐答案

您可以使用 Docker 1.17 中引入的多阶段构建功能

You can, with the multi-stage builds feature introduced in Docker 1.17

看看这个:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

然后正常构建镜像:

docker build -t alexellis2/href-counter:latest

来自:https://docs.docker.com/develop/develop-images/multistage-build/

最终结果是与以前相同的微小生产图像,但复杂性显着降低.您无需创建任何中间图像,也无需将任何工件提取到本地系统.

The end result is the same tiny production image as before, with a significant reduction in complexity. You don’t need to create any intermediate images and you don’t need to extract any artifacts to your local system at all.

它是如何工作的?第二条 FROM 指令以 alpine:latest 镜像为基础开始一个新的构建阶段.COPY --from=0 行仅将上一阶段的构建工件复制到这个新阶段.Go SDK 和任何中间工件都会被留下,不会保存在最终图像中.

How does it work? The second FROM instruction starts a new build stage with the alpine:latest image as its base. The COPY --from=0 line copies just the built artifact from the previous stage into this new stage. The Go SDK and any intermediate artifacts are left behind, and not saved in the final image.

这篇关于有没有办法将 Docker 镜像合并到 1 个容器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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