我正在使用 Buildkit 探索 docker 构建过程。在调用 docker build
之前,我通过在 CLI 上设置环境变量 DOCKER_BUILDKIT=1
来启用它。来自 buildkit documentation它说,
- 要使用外部 Dockerfile 前端,您的 Dockerfile 的第一行需要是
# syntax=docker/dockerfile:1
指向您要使用的特定镜像。
从语法 documentation它说,
- 此功能仅在使用 BuildKit 后端时可用,在使用经典构建器后端时被忽略。
这个 frontend 和 backend 在 docker 构建过程中意味着什么?谁能帮我简单理解一下?
最佳答案
TLDR; 前端和后端的概念是由 Buildkit 诞生的,之前在 docker 中是不存在的。前端就像一个将用户文件(例如:Dockerfile)转换为 LLB 的编译器。后端以最有效的方式执行 LLB 构建 docker 镜像。
历史
如果没有 Buildkit,则直接使用 Dockerfile 中的指令构建 Docker 镜像。没有创建这些指令的中间表示。指令被传递给 docker 引擎(也称为 Moby 引擎或 classic builder),然后构建镜像。
然后意识到,为了进一步改进和优化构建过程,构建操作的大部分基础都必须重新定义。因此,proposal为创建新引擎而生,Buildkit 伴随着构建过程的前后端分离而诞生。
One of the main design goals of buildkit is to separate frontend and backend concerns during a build process. A frontend is something designed for the users to describe their build definition. Backend solves the problem of finding a most efficient way to solve a common low-level description of the build operations, that has been prepared for them by the frontends.
法学学士学位
前后端分离由LLB(low-leve builder)实现。
Everything about execution and caching of your builds is defined only in LLB.
前端
前端是在 BuildKit 中运行的组件,将任何构建定义(用户编写的文件)转换为 LLB。 BuildKit 通过指定:#syntax=...
支持从容器镜像动态加载前端。一个著名的前端是 dockerfile 前端,因为它与 docker 引擎一起使用。您可以使用以下命令指定此容器镜像:#syntax=docker/dockerfile:latest
。
还有很多其他的前端可以使用,例如 mockerfile前端:#syntax=r2d4/mocker
。与通常的 Dockerfile 语法相比,这允许您使用稍微不同的语法。
后端
Buildkit 后端解决由各种前端生成的 LLB。由于 LLB 是一个依赖图,它可以被处理为:检测并跳过执行未使用的构建阶段,并行构建独立的构建阶段等。这就是为什么 Buildkit 能够在旧的构建过程中提高性能、存储管理等。此外,缓存模型已完全重写。
The core part of the builder(Buildkit) is a solver that takes a DAG of low-level build(LLB) instructions from the frontend and finds a way to execute them in a most efficient manner while keeping the cache for the next invocations.
要使用 Buildkit 后端,请指定DOCKER_BUILDKIT=1
。
Starting with version 18.09, Docker supports a new backend for executing your builds that is provided by the moby/buildkit project.
Moby 引擎(经典构建器)可以称为原始后端,但请记住它不使用 LLB,因此它的构建过程没有前端和后端。 p>
引用资料和资源:
- The original proposal to create Buildkit by Tonis Tiigi
- Tonis's introductory blog about Buildkit with an intro to LLB
- An article on how docker build works internally without buildkit
- A Docker blog with content on Buildkit, frontends, and LLB
- Mockerfile website, which includes code on how to generate LLB
- Dockerfile reference
关于docker - docker buildkit 的前端和后端到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73067660/