我知道新的 multi-stage build功能,它可以很好地与 Docker Compose 配合使用。但是,假设我坚持使用构建器模式(不要问)......有没有办法让 docker-compose up
使用构建器模式所需的构建脚本?
考虑链接文章中的相同构建器模式文件:
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 .
文件
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY app .
CMD ["./app"]
编译文件
#!/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 文件,但我不知道如何
cp
来自临时 Docker 容器的文件。docker-compose.yml
version: '3'
services:
app:
build: .
depends_on:
- app-build
app-build:
build:
context: .
dockerfile: Dockerfile.build
我可以构建临时 Docker 镜像/容器并运行
cp
通过使用 build.sh
的第一部分脚本,然后使用精简的撰写文件,但是,那么,我还不如坚持使用脚本。
最佳答案
据我了解,您问是否可以使用类似 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
已完成其工作,工件已准备就绪。使用共享卷有点冒险。你必须知道你在做什么。
Multiple containers can also share one or more data volumes. However, multiple containers writing to a single shared volume can cause data corruption. https://docs.docker.com/engine/tutorials/dockervolumes/#important-tips-on-using-shared-volumes
github 上有一个标题为“将
copy
添加到 yaml 配置”的问题:https://github.com/docker/compose/issues/1664我想在撰写文件中有这样的选项。您可以在上面的链接中查看其他人对此的看法。该问题现已关闭,但可能会再次打开。
关于docker - 我可以将 Docker Compose 与使用构建器模式的图像一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152216/