Docker

S2.5:多阶段镜像创建的意义与用法(Multi-Stage D

2020-01-09  本文已影响0人  睦月MTK

声明:所有的实验示例大部分来自《learn-docker-in-a-month-of-lunches》的作者Elton Stoneman,但运行结果并不都是照搬,大部分实验结果可能与原书不同


一、Q&A
  1. Q:为什么需要多阶段的镜像创建?
    A:因为使用多阶段的镜像创建后,可以不需要在服务器上安装诸多构建工具了,或者可以省去维护构建服务器的负担,让生产环境中只包含代码构建之后的东西以及运行它的最低需求环境。同时还可以减小最终镜像的大小,减少无意义的资源占用。
  2. Q:如何实现多阶段的镜像创建?
    A:使用Multi-Stage Dockerfile来创建镜像,它能够把镜像的过程分为多个阶段,比如分为构建阶段、测试阶段、生产阶段,构建阶段包含诸多构建代码所需要的工具,在构建成功后,便进入测试阶段,测试阶段会复制构建阶段生成的代码,放到测试阶段的镜像上去(包含诸多测试工具),测试通过后,进入生产阶段,该阶段只包含运行构建后代码最低的需求环境,这大大减少了无意义资源的占用(诸多构建工具以及测试工具等等)

二、Multi-Stage Dockerfile
  1. 解读Multi-Stage Dockerfile语法
cd diamol/ch04/exercises/multi-stage
$ cat Dockerfile
FROM diamol/base AS build-stage
RUN echo 'Building...' > /build.txt

FROM diamol/base AS test-stage
COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' >> /build.txt

FROM diamol/base
COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt

每一个阶段由FROM命令引导,as后接阶段的名字,每一个阶段都会由自己的文件系统,里面包含该阶段的东西,虽然该例中有三个不同的阶段,但最终的镜像只会有一个,即最后那个阶段的镜像。
每一个阶段都是独立的,但你可以从主机的文件系统中复制文件或者从上一个阶段的文件系统中复制文件(COPY --from=<last-stage>

  1. 创建镜像并且分析多阶段镜像的生成过程
$ docker image build --tag multi-stage .
ending build context to Docker daemon  2.048kB
Step 1/8 : FROM diamol/base AS build-stage
 ---> 055936d39205
Step 2/8 : RUN echo 'Building...' > /build.txt
 ---> Running in 27453d8b3bf5
Removing intermediate container 27453d8b3bf5
 ---> 837b01823689
Step 3/8 : FROM diamol/base AS test-stage
 ---> 055936d39205
Step 4/8 : COPY --from=build-stage /build.txt /build.txt
 ---> 6cf0aae463fa
Step 5/8 : RUN echo 'Testing...' >> /build.txt
 ---> Running in 19f8545c7ab1
Removing intermediate container 19f8545c7ab1
 ---> c7ef35085c2c
Step 6/8 : FROM diamol/base
 ---> 055936d39205
Step 7/8 : COPY --from=test-stage /build.txt /build.txt
 ---> 090ac3918240
Step 8/8 : CMD cat /build.txt
 ---> Running in c869b211a14e
Removing intermediate container c869b211a14e
 ---> 49688ae2741c
Successfully built 49688ae2741c
Successfully tagged multi-stage:latest

第1步到第2步是build-stage,该阶段会在build.txt中写入Building...
第3步到第5步是test-stage,该阶段会复制build-stage中build.txt然后继续写入Testing...
第6步到第8步是final-stage,该阶段会复制test-stage中的build.txt然后执行命令cat /build.txt

  1. 分析对比不使用Multi-Stage Dockerfile创建的镜像和使用了Multi-Stage Dockerfile创建的镜像的状态
    • 不使用Multi-Stage Dockerfile创建的镜像
    $ cd diamol/ch04/lab
    $ cat Dockerfile
    FROM diamol/golang
    
    WORKDIR web
    COPY index.html .
    COPY main.go .
    
    RUN go build -o /web/server
    RUN chmod +x /web/server
    
    CMD ["/web/server"]
    ENV USER=sixeyed
    EXPOSE 80
    $ docker image build --tag c4lab_or .
    $ docker image ls -f reference=c4lab_or
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    c4lab_or            latest              6e779f55b2c5        5 minutes ago       831MB
    
    • 使用Multi-Stage Dockerfile创建的镜像
    $ cp -f Dockerfile.optimized Dockerfile
    $ cat Dockerfile
    FROM diamol/golang AS builder
    
    COPY main.go .
    RUN go build -o /server
    RUN chmod +x /server
    
    # app
    FROM diamol/base
    
    EXPOSE 80
    CMD ["/web/server"]
    ENV USER="sixeyed"
    
    WORKDIR web
    COPY --from=builder /server .
    COPY index.html .
    $ docker image build --tag c4lab .
    $ docker image ls -f reference=c4lab
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    c4lab               latest              ba6b67c69a79        2 minutes ago       15.3MB
    

    可以看到,两个镜像大小相差715MB左右!!!

  2. 如何尽可能的优化Multi-Stage Dockerfile
    1. 尽可能的分清阶段,把无关的文件留在上一个阶段,最终阶段的镜像越小越好
    2. 普通Dockerfile的优化规则

参考文档:
[1] learn-docker-in-a-month-of-lunches
[2] 官方文档
附:
[1] Elton Stoneman的github项目

上一篇下一篇

猜你喜欢

热点阅读