docker - 有没有办法将 Docker 镜像组合到 1 个容器中?

标签 docker dockerfile docker-image

我现在有一些 Dockerfile。

一个用于 Cassandra 3.5,它是 FROM cassandra:3.5

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

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

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

也许,我不明白 Docker 是如何工作的,但我的 Scala 程序有 Cassandra 和 Kafka 作为依赖项,出于开发目的,我希望其他人能够使用 Dockerfile 简单地克隆我的 repo然后能够使用 Cassandra、Kafka、Scala、Java 和 SBT 构建它,这样他们就可以编译源代码。不过,我对此有很多问题。

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

最佳答案

您可以使用 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.

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 - 有没有办法将 Docker 镜像组合到 1 个容器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39626579/

相关文章:

typescript - 尝试在Dockerfile中编译TypeScript文件失败

docker - 如何使用 zsh 使用 docker exec

docker - 没有互联网的 Alpine Linux 安装包(docker)

docker - Visual Studio 生成的 Dockerfile 不适用于手动 docker build

docker - 如何更正 makefile 中的 docker,它需要至少 1 个参数才能删除所有容器命令

gradle - 如何使用Gradle从包含TGZ的TAR中提取文件?

docker - 使用commit docker命令的最佳情况是什么?

Docker 'latest' 标记和推送

docker - git pull、docker-compose build 和 docker-compose up -d 是在空机上部署完整解决方案的好方法吗

go - docker run 上的 Docker Golang onbuild 自定义操作