您可以使用Docker 1.17中引入的 多阶段构建 功能
看看这个:
FROM golang:1.7.3WORKDIR /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-certificatesWORKDIR /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/
最终结果是与以前相同的微小生产图像,并大大降低了复杂性。您无需创建任何中间映像,也不需要将任何工件提取到本地系统。
它是如何工作的?第二条FROM指令以alpine:latest图像为基础开始新的构建阶段。COPY –from =
0行仅将之前阶段的构建工件复制到此新阶段。Go SDK和任何中间工件都被保留了下来,没有保存在最终图像中。



