使用 BuildKit 构建镜像

    注意:如果您的镜像构建使用的是云服务商提供的镜像构建服务(Docker Hub 自动构建、腾讯云容器服务、阿里云容器服务等),由于上述服务提供商的 Docker 版本低于 18.09,BuildKit 无法使用,将造成镜像构建失败。建议使用 BuildKit 构建镜像时使用一个新的 Dockerfile 文件(例如 Dockerfile.buildkit)

    注意:docker-compose build 命令暂时不支持 BuildKit

    启用 BuildKit 之后,我们可以使用下面几个新的 Dockerfile 指令来加快镜像构建。

    目前,几乎所有的程序都会使用依赖管理工具,例如 Go 中的 go modNode.js 中的 npm 等等,当我们构建一个镜像时,往往会重复的从互联网中获取依赖包,难以缓存,大大降低了镜像的构建效率。

    例如一个前端工程需要用到 npm

    为解决这个问题,进一步的我们可以设想一个类似 数据卷 的功能,在镜像构建时把 node_modules 文件夹挂载上去,在构建完成后,这个 node_modules 文件夹会自动卸载,实际的镜像中并不包含 node_modules 这个文件夹,这样我们就省去了每次获取依赖的时间,大大增加了镜像构建效率,同时也避免了生成了大量的中间层镜像。

    BuildKit 提供了 RUN --mount=type=cache 指令,可以实现上边的设想。

    1. # syntax = docker/dockerfile:experimental
    2. FROM node:alpine as builder
    3. WORKDIR /app
    4. COPY package.json /app/
    5. RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
    6. COPY src /app/src
    7. RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
    8. # --mount=type=cache,target=/app/dist,id=my_app_dist,sharing=locked \
    9. npm run build
    10. FROM nginx:alpine
    11. # COPY --from=builder /app/dist /app/dist
    12. # 为了更直观的说明 from 和 source 指令,这里使用 RUN 指令
    13. RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
    14. # --mount=type=cache,target/tmp/dist,from=my_app_dist,sharing=locked \

    由于 BuildKit 为实验特性,每个 Dockerfile 文件开头都必须加上如下指令

    1. # syntax = docker/dockerfile:experimental

    第一个 RUN 指令执行后,idmy_app_npm_module 的缓存文件夹挂载到了 /app/node_modules 文件夹中。多次执行也不会产生多个中间层镜像。

    第二个 RUN 指令执行时需要用到 node_modules 文件夹,node_modules 已经挂载,命令也可以正确执行。

    上面的 Dockerfile--mount=type=cache,... 中指令作用如下:

    该指令可以将一个镜像(或上一构建阶段)的文件挂载到指定位置。

    该指令可以将一个 tmpfs 文件系统挂载到指定位置。

    1. # syntax = docker/dockerfile:experimental
    2. RUN --mount=type=tmpfs,target=/temp \
    3. mount | grep /temp

    该指令可以将一个文件(例如密钥)挂载到指定位置。

    1. # syntax = docker/dockerfile:experimental
    2. RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
    3. cat /root/.aws/credentials

    该指令可以挂载 ssh 密钥。

    1. # syntax = docker/dockerfile:experimental
    2. FROM alpine
    3. RUN apk add --no-cache openssh-client
    4. RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    5. RUN --mount=type=ssh ssh git@gitlab.com | tee /hello
    1. $ eval $(ssh-agent)
    2. $ ssh-add ~/.ssh/id_rsa

    官方文档