Dockerfile多阶段构建

要求

从Docker 17.05版本开始,使用docker build命令开始支持多阶段构建。

之前解决方案

比如我想把自己开发的一个app.go,我想在自己最终的容器中使用编译过的app可执行文件,但是我有不想在最终的容器中保存一些不需要的东西,这时我需要:

  1. 构建一个中间辅助容器,然后在里面将我开发的app.go编译好,然后从容器中拷贝到本地目录,然后将中间服务容器删除掉
  2. 重新构建一个容器,将之前编译好,并且在本地目录的可执行文件拷贝到新容器中,那么我最终的容器就没有一些不需要的东西了,保证了容器尽量小

中间辅助容器的Dockerfile.build内容如下

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
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"] 

中间构建容器,拷贝文件的操作放到一个脚本中,内容如下

#!/bin/sh
echo Building alexellis2/href-counter:build

docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \  
    -t alexellis2/href-counter:build . -f Dockerfile.build

docker container create --name extract alexellis2/href-counter:build  
docker container cp extract:/go/src/github.com/alexellis/href-counter/app ./app  
docker container rm -f extract

echo Building alexellis2/href-counter:latest

docker build --no-cache -t alexellis2/href-counter:latest .
rm ./app

Dockerfile多阶段构建

多阶段构建的Dockerfile中可以有多个FROM指令,每个FROM指令中可以使用不同的基础镜像。每一个FROM代表一个构建阶段。可以在不同的阶段之间进行文件的拷贝,从而保证只将自己需要的文件拷贝到最终的镜像中,实现镜像尽量小的要求。

支持多阶段构建的Dockerfile内容如下

FROM golang:1.7.3 as builder
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=builder /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]

构建最终镜像只需要执行一条docker build -t alexellis2/href-counter:latest .命令就可以了,不再需要中间辅助脚本

参考文献

转载于:https://my.oschina.net/alazyer/blog/1828993

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值