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

查看:25
本文介绍了有没有办法将 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.

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

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 简单地克隆我的 repo然后能够使用 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天全站免登陆