我知道新的multi-stage build功能,可与Docker Compose很好地配合使用。但是,比方说,我坚持使用builder-pattern(不要问)...有什么方法可以让docker-compose up使用builder所需的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 .

Docker文件
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容器中对文件进行cp

docker-compose.yml
version: '3'
services:
  app:
    build: .
    depends_on:
     - app-build
  app-build:
    build:
      context: .
      dockerfile: Dockerfile.build

我可以通过从上方使用cp脚本的第一部分,然后使用精简后的撰写文件来构建临时Docker镜像/容器并运行build.sh,但是,那么,我还是坚持使用该脚本。

最佳答案

一种方法可以使用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 .

但是,我建议您采用“多阶段构建”方法。比这简单得多。

10-07 21:10