有没有一种方法可以将Docker映像合并到一个容器中? [英] Is there a way to combine Docker images into 1 container?

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

问题描述

我现在有几个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中引入的功能

看看这个:

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映像合并到一个容器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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