moby/buildkit

GitHub: moby/buildkit

BuildKit 是一个高效、缓存优化的构建工具包,用于将源代码转换为构建工件。

Stars: 10030 | Forks: 1433

[![asciicinema 示例](https://asciinema.org/a/gPEIEo1NzmDTUu2bEPsUboqmU.png)](https://asciinema.org/a/gPEIEo1NzmDTUu2bEPsUboqmU) # 构建工具 [![GitHub 发布](https://img.shields.io/github/release/moby/buildkit.svg?style=flat-square)](https://github.com/moby/buildkit/releases/latest) [![PkgGoDev](https://img.shields.io/badge/go.dev-docs-007d9c?style=flat-square&logo=go&logoColor=white)](https://pkg.go.dev/github.com/moby/buildkit/client/llb) [![CI BuildKit 状态](https://img.shields.io/github/actions/workflow/status/moby/buildkit/buildkit.yml?label=buildkit&logo=github&style=flat-square)](https://github.com/moby/buildkit/actions?query=workflow%3Abuildkit) [![CI 前端状态](https://img.shields.io/github/actions/workflow/status/moby/buildkit/frontend.yml?label=frontend&logo=github&style=flat-square)](https://github.com/moby/buildkit/actions?query=workflow%3Afrontend) [![Go Report Card](https://goreportcard.com/badge/github.com/moby/buildkit?style=flat-square)](https://goreportcard.com/report/github.com/moby/buildkit) [![Codecov](https://img.shields.io/codecov/c/github/moby/buildkit?logo=codecov&style=flat-square)](https://codecov.io/gh/moby/buildkit) BuildKit 是一个工具包,用于以高效、表达性和可重复的方式将源代码转换为构建工件。 主要功能: - 自动垃圾回收 - 可扩展的前端格式 - 并发依赖解析 - 高效的指令缓存 - 构建缓存导入/导出 - 嵌套构建作业调用 - 可分发的工作者 - 多种输出格式 - 可插拔架构 - 无需 root 权限执行 阅读提案:https://github.com/moby/moby/issues/32925 介绍性博客文章:https://blog.mobyproject.org/introducing-buildkit-17e056cc5317 加入 [Docker Community Slack](https://dockr.ly/comm-slack) 上的 `#buildkit` 频道 - [使用情况](#used-by) - [快速入门](#quick-start) - [Linux 设置](#linux-setup) - [Windows 设置](#windows-setup) - [macOS 设置](#macos-setup) - [从源代码构建](#build-from-source) - [探索 LLB](#exploring-llb) - [探索 Dockerfile](#exploring-dockerfiles) - [使用 `buildctl` 构建 Dockerfile](#building-a-dockerfile-with-buildctl) - [使用外部前端构建 Dockerfile](#building-a-dockerfile-using-external-frontend) - [输出](#output) - [镜像/注册表](#imageregistry) - [本地目录](#local-directory) - [Docker tarball](#docker-tarball) - [OCI tarball](#oci-tarball) - [containerd 镜像存储](#containerd-image-store) - [缓存](#cache) - [垃圾回收](#garbage-collection) - [导出缓存](#export-cache) - [内联(推送镜像和缓存一起)](#inline-push-image-and-cache-together) - [注册表(分别推送镜像和缓存)](#registry-push-image-and-cache-separately) - [本地目录](#local-directory-1) - [GitHub Actions 缓存(实验性)](#github-actions-cache-experimental) - [S3 缓存(实验性)](#s3-cache-experimental) - [Azure Blob Storage 缓存(实验性)](#azure-blob-storage-cache-experimental) - [一致性哈希](#consistent-hashing) - [元数据](#metadata) - [Systemd 套接字激活](#systemd-socket-activation) - [将 BuildKit 作为 TCP 服务公开](#expose-buildkit-as-a-tcp-service) - [负载均衡](#load-balancing) - [容器化 BuildKit](#containerizing-buildkit) - [Podman](#podman) - [Nerdctl](#nerdctl) - [Kubernetes](#kubernetes) - [无守护进程](#daemonless) - [OpenTelemetry 支持](#opentelemetry-support) - [无需 root 权限运行 BuildKit](#running-buildkit-without-root-privileges) - [构建多平台镜像](#building-multi-platform-images) - [配置 `buildctl`](#configuring-buildctl) - [颜色输出控制](#color-output-controls) - [日志行数(用于 tty 模式中的活动步骤)](#number-of-log-lines-for-active-steps-in-tty-mode) - [贡献](#contributing) ## 使用情况 BuildKit 被以下项目使用: - [Moby & Docker](https://github.com/moby/moby/pull/37151) (`DOCKER_BUILDKIT=1 docker build`) - [img](https://github.com/genuinetools/img) - [OpenFaaS Cloud](https://github.com/openfaas/openfaas-cloud) - [容器构建接口](https://github.com/containerbuilding/cbi) - [Tekton Pipelines](https://github.com/tektoncd/catalog)(以前称为 [Knative Build Templates](https://github.com/knative/build-templates)) - [Sanic 构建工具](https://github.com/distributed-containers-inc/sanic) - [vab](https://github.com/stellarproject/vab) - [Rio](https://github.com/rancher/rio) - [kim](https://github.com/rancher/kim) - [PouchContainer](https://github.com/alibaba/pouch) - [Docker buildx](https://github.com/docker/buildx) - [Okteto Cloud](https://okteto.com/) - [Earthly earthfiles](https://github.com/vladaionescu/earthly) - [Gitpod](https://github.com/gitpod-io/gitpod) - [Dagger](https://dagger.io) - [envd](https://github.com/tensorchord/envd/) - [Depot](https://depot.dev) - [Namespace](https://namespace.so) - [Unikraft](https://unikraft.org) - [DevZero](https://devzero.io) - [dacc](https://github.com/r2d4/dacc) ## 快速入门 :information_source: 对于 Kubernetes 部署,请参阅 `examples/kubernetes`。 BuildKit 由 `buildkitd` 守护进程和 `buildctl` 客户端组成。 虽然 `buildctl` 客户端适用于 Linux、macOS 和 Windows,但 `buildkitd` 守护进程目前仅适用于 Linux 和 *Windows*。 BuildKit 的最新二进制文件可在以下位置获取:[此处](https://github.com/moby/buildkit/releases)(适用于 Linux、macOS 和 Windows)。 ### Linux 设置 `buildkitd` 守护进程需要安装以下组件: - [runc](https://github.com/opencontainers/runc) 或 [crun](https://github.com/containers/crun) - [containerd](https://github.com/containerd/containerd)(如果您想使用 containerd 工作者) **启动 `buildkitd` 守护进程:** 您需要以 root 用户身份在主机上运行 `buildkitd`。 ``` $ sudo buildkitd ``` 要作为非 root 用户运行 `buildkitd`,请参阅 `docs/rootless.md`。 buildkitd 守护进程支持两种工作者后端:OCI(runc)和 containerd。 默认情况下,使用 OCI(runc)工作者。您可以将 `--oci-worker=false --containerd-worker=true` 设置为使用 containerd 工作者。 我们欢迎添加更多后端。 要使用 systemd 套接字激活启动 buildkitd 守护进程,您可以安装 buildkit systemd 单元文件。 请参阅 [Systemd 套接字激活](#systemd-socket-activation) buildkitd 守护进程默认在 `/run/buildkit/buildkitd.sock` 上监听 gRPC API,但您也可以使用 TCP 套接字。 请参阅 [将 BuildKit 作为 TCP 服务公开](#expose-buildkit-as-a-tcp-service). ### Windows 设置 请参阅 `docs/windows.md` 中的说明和注意事项。 ### macOS 设置 [Homebrew 公式](https://formulae.brew.sh/formula/buildkit)(非官方)适用于 macOS。 ``` $ brew install buildkit ``` Homebrew 公式不包含守护进程(`buildkitd`)。 例如,可以使用 [Lima](https://lima-vm.io) 在 Linux 虚拟机内部启动守护进程。 ``` brew install lima limactl start template://buildkit export BUILDKIT_HOST="unix://$HOME/.lima/buildkit/sock/buildkitd.sock" ``` ### 从源代码构建 要使用源代码构建 BuildKit,请参阅 `.github/CONTRIBUTING.md`。 有关 `buildctl` 参考,请参阅 [此文档](./docs/reference/buildctl.md). ### 探索 LLB BuildKit 构建基于名为 LLB 的二进制中间格式,用于定义运行构建部分的过程的依赖图。tl;dr:LLB 对 Dockerfile,就像 LLVM IR 对 C。 - 作为 Protobuf 消息打包 - 可并发执行 - 高效缓存 - 厂商中立(即,非 Dockerfile 语言可以轻松实现) 有关格式定义,请参阅 `solver/pb/ops.proto`,有关示例 LLB 应用程序,请参阅 `./examples/README.md`。 目前,以下高级语言已实现 LLB: - Dockerfile(请参阅 [探索 Dockerfile](#exploring-dockerfiles)) - [Buildpacks](https://github.com/tonistiigi/buildkit-pack) - [Mockerfile](https://matt-rickard.com/building-a-new-dockerfile-frontend/) - [Gockerfile](https://github.com/po3rin/gockerfile) - [bldr (Pkgfile)](https://github.com/talos-systems/bldr/) - [HLB](https://github.com/openllb/hlb) - [Earthfile (Earthly)](https://github.com/earthly/earthly) - [Cargo Wharf (Rust)](https://github.com/denzp/cargo-wharf) - [Nix](https://github.com/reproducible-containers/buildkit-nix) - [mopy (Python)](https://github.com/cmdjulian/mopy) - [envd (starlark)](https://github.com/tensorchord/envd/) - [Blubber](https://gitlab.wikimedia.org/repos/releng/blubber) - [Bass](https://github.com/vito/bass) - [kraft.yaml (Unikraft)](https://github.com/unikraft/kraftkit/tree/staging/tools/dockerfile-llb-frontend) - [r2d4/llb (JSON Gateway)](https://github.com/r2d4/llb) - [Massé](https://github.com/marxarelli/masse) - [DALEC](https://github.com/project-dalec/dalec) - [Dexfile](https://github.com/dexnore/dexfile) - (通过打开 PR 添加您自己的语言) ### 探索 Dockerfile 前端是运行在 BuildKit 内部的组件,将任何构建定义转换为 LLB。有一个特殊的前端称为网关 (`gateway.v0`),允许使用任何镜像作为前端。 在开发过程中,Dockerfile 前端 (`dockerfile.v0`) 也是 BuildKit 仓库的一部分。在未来,这将被移出,并且可以使用外部镜像构建 Dockerfile。 #### 使用 `buildctl` 构建 Dockerfile ``` buildctl build \ --frontend=dockerfile.v0 \ --local context=. \ --local dockerfile=. # 或者 buildctl build \ --frontend=dockerfile.v0 \ --local context=. \ --local dockerfile=. \ --opt target=foo \ --opt build-arg:foo=bar ``` `--local` 将本地源文件从客户端暴露给构建器。`context` 和 `dockerfile` 是 Dockerfile 前端查找构建上下文和 Dockerfile 位置的名称。 如果 Dockerfile 有不同的文件名,可以使用 `--opt filename=./Dockerfile-alternative` 指定。 #### 使用外部前端构建 Dockerfile Dockerfile 前端的外部版本已推送到 https://hub.docker.com/r/docker/dockerfile-upstream 和 https://hub.docker.com/r/docker/dockerfile,并可以使用网关前端使用。外部前端源目前位于 `./frontend/dockerfile/cmd/dockerfile-frontend`,但将来将移出此存储库([#163](https://github.com/moby/buildkit/issues/163))。要自动从此存储库的 master 分支构建,可以使用 `docker/dockerfile-upstream:master` 或 `docker/dockerfile-upstream:master-labs` 镜像。 ``` buildctl build \ --frontend gateway.v0 \ --opt source=docker/dockerfile \ --local context=. \ --local dockerfile=. buildctl build \ --frontend gateway.v0 \ --opt source=docker/dockerfile \ --opt context=https://github.com/moby/moby.git \ --opt build-arg:APT_MIRROR=cdn-fastly.deb.debian.org ``` ### 输出 默认情况下,构建结果和中间缓存将仅保留在 BuildKit 内部。需要指定输出才能检索结果。 #### 镜像/注册表 ``` buildctl build ... --output type=image,name=docker.io/username/image,push=true ``` 要导出图像到多个注册表: ``` buildctl build ... --output type=image,\"name=docker.io/username/image,docker.io/username2/image2\",push=true ``` 要将缓存嵌入到图像中并将其与注册表一起推送,请输入 `registry` 是必需的以导入缓存,您应指定 `--export-cache type=inline` 和 `--import-cache type=registry,ref=...`。要将缓存导出到本地目录,您应指定 `--export-cache type=local`。 有关详细信息,请参阅 [导出缓存](#export-cache).
标签:Docker, EVTX分析, Go 语言, 分布式工作, 前端格式, 可扩展性, 可重复性, 多输出格式, 安全防御评估, 工作流管理, 指令缓存, 插件架构, 效率提升, 无根权限执行, 日志审计, 构建缓存, 源代码构建, 缓存优化, 自动化构建, 请求拦截