我可以使用Docker Compose和使用构build器模式的图像吗?
我知道新的多阶段构buildfunction,它与Docker Compose很好地协作。 然而,让我们说,我坚持build设者模式(不要问)…有没有什么办法让docker-compose up
使用build设者模式所需的构build脚本?
考虑链接文章中的相同构build器模式文件:
Dockerfile.build
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 go get -d -v golang.org/x/net/html \ && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
Dockerfile
FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY app . CMD ["./app"]
build.sh
#!/bin/sh docker build -t alexellis2/href-counter:build . -f Dockerfile.build docker create --name extract alexellis2/href-counter:build docker cp extract:/go/src/github.com/alexellis/href-counter/app ./app docker rm -f extract docker build --no-cache -t alexellis2/href-counter:latest . rm ./app
我可以像这样构造一个Docker Compose文件,但我不知道如何从临时Docker容器中获取文件。
泊坞窗,compose.yml
version: '3' services: app: build: . depends_on: - app-build app-build: build: context: . dockerfile: Dockerfile.build
我可以构build临时的Docker镜像/容器,并使用上面的build.sh
脚本的第一部分运行cp
,然后使用精简的组合文件,但是,我可能只是坚持使用脚本。
一种方法可以使用2个docker-compose调用,并结合一个目录映射:
version: '3' services: app: build: . app-build: build: context: . dockerfile: Dockerfile.build volumes: - ./build/:/go/src/github.com/alexellis/href-counter/
然后:
#This will produce local ./build/app artifact docker-compose build app-build #Having the previous artifact, will use it: docker-compose build app
只需将其更改为Dockerfile:
COPY build/app .
不过,我build议你使用多阶段构build方法。 比这更简单。
据我了解,你问是否有可能在撰写文件中使用像docker cp
这样的东西从临时容器中提取工件。
那么,使用共享volume
是一个选项。
version: '3' services: app: build: . depends_on: - app-build volumes: - shared:/source app-build: build: context: . dockerfile: Dockerfile.build volumes: - shared:/output volumes: - shared:
但是你必须在app
服务中添加一个脚本,它将检查app-build
是否已完成其工作,并且工件已准备就绪。
使用共享卷有点危险。 你必须知道你在做什么。
多个容器也可以共享一个或多个数据卷。 但是,写入单个共享卷的多个容器可能会导致数据损坏。 https://docs.docker.com/engine/tutorials/dockervolumes/#important-tips-on-using-shared-volumes
在github上有一个标题为“添加copy
到yamlconfiguration”的问题: https : //github.com/docker/compose/issues/1664
我想在撰写文件中有这样的选项。 您可以在上面的链接中查看他人的意见。 这个问题现在是closures的,但可能会再次打开。
Docker撰写没有构build依赖关系,这些是用于确定容器后构build的启动顺序。 而且它不允许你创build一个容器并复制一个文件在另一个版本中使用。 因此,如果您的环境至less运行17.05,则剩下上面的构build脚本或使用新的多阶段构build。