多阶段构建

之前的做法

在 Docker 17.05 版本之前,我们构建 Docker 镜像时,通常会采用两种方式:

全部放入一个 Dockerfile

一种方式是将所有的构建过程编包含在一个 Dockerfile 中,包括项目及其依赖库的编译、测试、打包等流程,这里可能会带来的一些问题:

  • Dockerfile 特别长,可维护性降低

  • 镜像层次多,镜像体积较大,部署时间变长

  • 源代码存在泄露的风险

例如

编写 app.go 文件,该程序输出 Hello World!

  1. package main
  2. import "fmt"
  3. func main(){
  4. fmt.Printf("Hello World!");
  5. }

编写 Dockerfile.one 文件

  1. FROM golang:1.9-alpine
  2. RUN apk --no-cache add git ca-certificates
  3. WORKDIR /go/src/github.com/go/helloworld/
  4. COPY app.go .
  5. RUN go get -d -v github.com/go-sql-driver/mysql \
  6. && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
  7. && cp /go/src/github.com/go/helloworld/app /root
  8. WORKDIR /root/
  9. CMD ["./app"]

构建镜像

  1. $ docker build -t go/helloworld:1 -f Dockerfile.one .

分散到多个 Dockerfile

另一种方式,就是我们事先在一个 Dockerfile 将项目及其依赖库编译测试打包好后,再将其拷贝到运行环境中,这种方式需要我们编写两个 Dockerfile 和一些编译脚本才能将其两个阶段自动整合起来,这种方式虽然可以很好地规避第一种方式存在的风险,但明显部署过程较复杂。

例如

编写 Dockerfile.build 文件

  1. FROM golang:1.9-alpine
  2. RUN apk --no-cache add git
  3. WORKDIR /go/src/github.com/go/helloworld
  4. COPY app.go .
  5. RUN go get -d -v github.com/go-sql-driver/mysql \
  6. && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

编写 Dockerfile.copy 文件

  1. FROM alpine:latest
  2. RUN apk --no-cache add ca-certificates
  3. WORKDIR /root/
  4. COPY app .
  5. CMD ["./app"]

新建 build.sh

  1. #!/bin/sh
  2. echo Building go/helloworld:build
  3. docker build -t go/helloworld:build . -f Dockerfile.build
  4. docker create --name extract go/helloworld:build
  5. docker cp extract:/go/src/github.com/go/helloworld/app ./app
  6. docker rm -f extract
  7. echo Building go/helloworld:2
  8. docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copy
  9. rm ./app

现在运行脚本即可构建镜像

  1. $ chmod +x build.sh
  2. $ ./build.sh

对比两种方式生成的镜像大小

  1. $ docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
  4. go/helloworld 1 f55d3e16affc 2 minutes ago 295MB

使用多阶段构建

为解决以上问题,Docker v17.05 开始支持多阶段构建 (multistage builds)。使用多阶段构建我们就可以很容易解决前面提到的问题,并且只需要编写一个 Dockerfile

例如

编写 Dockerfile 文件

  1. FROM golang:1.9-alpine as builder
  2. RUN apk --no-cache add git
  3. WORKDIR /go/src/github.com/go/helloworld/
  4. RUN go get -d -v github.com/go-sql-driver/mysql
  5. COPY app.go .
  6. RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
  7. FROM alpine:latest as prod
  8. RUN apk --no-cache add ca-certificates
  9. WORKDIR /root/
  10. COPY --from=0 /go/src/github.com/go/helloworld/app .
  11. CMD ["./app"]

构建镜像

  1. $ docker build -t go/helloworld:3 .

对比三个镜像大小

  1. $ docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB
  4. go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
  5. go/helloworld 1 f55d3e16affc 2 minutes ago 295MB

很明显使用多阶段构建的镜像体积小,同时也完美解决了上边提到的问题。

只构建某一阶段的镜像

我们可以使用 as 来为某一阶段命名,例如

  1. FROM golang:1.9-alpine as builder

例如当我们只想构建 builder 阶段的镜像时,我们可以在使用 docker build 命令时加上 --target 参数即可

  1. $ docker build --target builder -t username/imagename:tag .

构建时从其他镜像复制文件

上面例子中我们使用 COPY --from=0 /go/src/github.com/go/helloworld/app . 从上一阶段的镜像中复制文件,我们也可以复制任意镜像中的文件。

  1. $ COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf

写于 2018年09月30日Docker 1229

如非特别注明,文章皆为原创。

转载请注明出处: https://www.liayal.com/article/5bb08e4ee452e5131dbb6049

记小栈小程序上线啦~搜索【记小栈】【点击扫码】体验

你不想说点啥么?
😀😃😄😁😆😅😂🤣☺️😊😇🙂🙃😉😌😍😘😗😙😚😋😜😝😛🤑🤗🤓😎🤡🤠😏😒😞😔😟😕🙁☹️😣😖😫😩😤😠😡😶😐😑😯😦😧😮😲😵😳😱😨😰😢😥🤤😭😓😪😴🙄🤔🤥😬🤐🤢🤧😷🤒🤕😈👿👹👺💩👻💀☠️👽👾🤖🎃😺😸😹😻😼😽🙀😿😾👐👐🏻👐🏼👐🏽👐🏾👐🏿🙌🙌🏻🙌🏼🙌🏽🙌🏾🙌🏿👏👏🏻👏🏼👏🏽👏🏾👏🏿🙏🙏🏻🙏🏼🙏🏽🙏🏾🙏🏿🤝👍👍🏻👍🏼👍🏽👍🏾👍🏿👎👎🏻👎🏼👎🏽👎🏾👎🏿👊👊🏻👊🏼👊🏽👊🏾👊🏿✊🏻✊🏼✊🏽✊🏾✊🏿

评论

~ 评论还没有,沙发可以有 O(∩_∩)O~