构建套件

概述

BuildKit 是一个改进的后端,用于替换旧版构建器。自版本 23.0 起,BuildKit 是 Docker Desktop 和 Docker Engine 上用户的默认构建器。

BuildKit 提供了新功能并提高了构建的性能。它还引入了对处理更复杂场景的支持:

除了许多新功能之外,BuildKit 在当前体验上改进的主要领域是性能、存储管理和可扩展性。从性能方面来看,一个重要的更新是新的完全并发构建图解算器。它可以在可能的情况下并行运行构建步骤,并优化对最终结果没有影响的命令。我们还优化了对本地源文件的访问。通过仅跟踪重复构建调用之间对这些文件所做的更新,无需等待读取或上传本地文件即可开始工作。

法学学士

BuildKit 的核心是 低级构建 (LLB)定义格式。 LLB 是一种中间二进制格式,允许开发人员扩展 BuildKit。 LLB 定义了一个内容可寻址的依赖图,可用于将非常复杂的构建定义放在一起。它还支持 Dockerfile 中未公开的功能,例如直接数据挂载和嵌套调用。

有关构建的执行和缓存的所有内容都在 LLB 中定义。与遗留构建器相比,缓存模型被完全重写。 LLB 不使用启发式方法来比较图像,而是直接跟踪构建图和安装到特定操作的内容的校验和。这使得它更快、更精确、更便携。构建缓存甚至可以导出到注册表,可以通过任何主机上的后续调用按需提取它。

LLB 可以使用golang 客户端包直接生成, 该客户端包允许使用 Go 语言原语定义构建操作之间的关系。这使您能够完全运行您可以想象的任何内容,但可能不会是大多数人定义其构建的方式。相反,大多数用户会使用前端组件或 LLB 嵌套调用来运行一组准备好的构建步骤。

前端

前端是一个组件,它采用人类可读的构建格式并将其转换为 LLB,以便 BuildKit 可以执行它。前端可以作为图像分发,并且用户可以针对前端的特定版本,该版本保证适用于其定义所使用的功能。

例如,要使用 BuildKit 构建 Dockerfile,您将 使用外部 Dockerfile 前端

入门

BuildKit 是 Docker Desktop 和 Docker Engine v23.0 及更高版本上的用户的默认构建器。

如果您已经安装了 Docker Desktop,则无需启用 BuildKit。如果您运行的 Docker 引擎版本早于 23.0,则可以通过设置环境变量或将 BuildKit 设置为守护程序配置中的默认设置来启用 BuildKit。

要在运行命令时设置 BuildKit 环境变量docker build ,请运行:

$ DOCKER_BUILDKIT=1 docker build .

笔记

Buildx始终使用 BuildKit。

要默认使用 Docker BuildKit,请按 /etc/docker/daemon.json如下所示编辑 Docker 守护进程配置,然后重新启动守护进程。

{
  "features": {
    "buildkit": true
  }
}

如果该/etc/docker/daemon.json文件不存在,请创建名为的新文件 daemon.json,然后将以下内容添加到该文件中。并重新启动 Docker 守护进程。

Windows 上的 BuildKit

警告

BuildKit 仅完全支持构建 Linux 容器。 Windows 容器支持是实验性的,并在 moby/buildkit#616.

自版本 0.13 起,BuildKit 对 Windows 容器 (WCOW) 提供了实验性支持。本部分将引导您完成尝试的步骤。我们特别感谢您通过在此处提出问题而提交的任何反馈 buildkitd.exe

已知的限制

  • Windows 上的 BuildKit 目前仅支持containerdWorker。对非 OCI 工作人员的支持在 moby/buildkit#4836中进行跟踪。

先决条件

  • 架构:amd64arm64(二进制文件可用,但尚未正式测试)。
  • 支持的操作系统:Windows Server 2019、Windows Server 2022、Windows 11。
  • 基础图像:ServerCore:ltsc2019, ServerCore:ltsc2022, NanoServer:ltsc2022.请参阅 此处的兼容性图
  • Docker 桌面版本 4.29 或更高版本

脚步

笔记

以下命令需要 PowerShell 终端中的管理员(提升的)权限。

  1. 启用Hyper-V容器Windows 功能。

    > Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V, Containers -All
    

    如果您看到RestartNeededTrue请重新启动计算机并以管理员身份重新打开 PowerShell 终端。否则,继续下一步。

  2. 切换到 Docker Desktop 中的 Windows 容器。

    选择任务栏中的 Docker 图标,然后切换到 Windows 容器...。

  3. 按照此处的安装说明安装 Containerd 版本 1.7.7 或更高版本 。

  4. 下载并解压最新的 BuildKit 版本。

    $version = "v0.13.1" # specify the release version, v0.13+
    $arch = "amd64" # arm64 binary available too
    curl.exe -LO https://github.com/moby/buildkit/releases/download/$version/buildkit-$version.windows-$arch.tar.gz
    # there could be another `.\bin` directory from containerd instructions
    # you can move those
    mv bin bin2
    tar.exe xvf .\buildkit-$version.windows-$arch.tar.gz
    ## x bin/
    ## x bin/buildctl.exe
    ## x bin/buildkitd.exe
  5. PATH.

    # after the binaries are extracted in the bin directory
    # move them to an appropriate path in your $Env:PATH directories or:
    Copy-Item -Path ".\bin" -Destination "$Env:ProgramFiles\buildkit" -Recurse -Force
    # add `buildkitd.exe` and `buildctl.exe` binaries in the $Env:PATH
    $Path = [Environment]::GetEnvironmentVariable("PATH", "Machine") + `
        [IO.Path]::PathSeparator + "$Env:ProgramFiles\buildkit"
    [Environment]::SetEnvironmentVariable( "Path", $Path, "Machine")
    $Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + `
        [System.Environment]::GetEnvironmentVariable("Path","User")
  6. 开始buildkitd.exe

    > buildkitd.exe
    time="2024-02-26T10:42:16+03:00" level=warning msg="using null network as the default"
    time="2024-02-26T10:42:16+03:00" level=info msg="found worker \"zcy8j5dyjn3gztjv6gv9kn037\", labels=map[org.mobyproject.buildkit.worker.containerd.namespace:buildkit org.mobyproject.buildkit.worker.containerd.uuid:c30661c1-5115-45de-9277-a6386185a283 org.mobyproject.buildkit.worker.executor:containerd org.mobyproject.buildkit.worker.hostname:[deducted] org.mobyproject.buildkit.worker.network: org.mobyproject.buildkit.worker.selinux.enabled:false org.mobyproject.buildkit.worker.snapshotter:windows], platforms=[windows/amd64]"
    time="2024-02-26T10:42:16+03:00" level=info msg="found 1 workers, default=\"zcy8j5dyjn3gztjv6gv9kn037\""
    time="2024-02-26T10:42:16+03:00" level=warning msg="currently, only the default worker can be used."
    time="2024-02-26T10:42:16+03:00" level=info msg="running server on //./pipe/buildkitd"
    
  7. 在另一个具有管理员权限的终端中,创建一个使用本地 BuildKit 守护程序的远程构建器。

    笔记

    这需要 Docker Desktop 版本 4.29 或更高版本。

    > docker buildx create --name buildkit-exp --use --driver=remote npipe:////./pipe/buildkitd
    buildkit-exp
    
  8. 通过运行验证构建器连接docker buildx inspect

    > docker buildx inspect
    Name:          buildkit-exp
     Driver:        remote
     Last Activity: 2024-04-15 17:51:58 +0000 UTC
     Nodes:
     Name:             buildkit-exp0
     Endpoint:         npipe:////./pipe/buildkitd
     Status:           running
     BuildKit version: v0.13.1
     Platforms:        windows/amd64
    ...
    
  9. 创建 Dockerfile 并构建hello-world镜像。

    > mkdir sample_dockerfile
    > cd sample_dockerfile
    > Set-Content Dockerfile @"
    FROM mcr.microsoft.com/windows/nanoserver:ltsc2022
    USER ContainerAdministrator
    COPY hello.txt C:/
    RUN echo "Goodbye!" >> hello.txt
    CMD ["cmd", "/C", "type C:\\hello.txt"]
    "@
    Set-Content hello.txt @"
    Hello from BuildKit!
    This message shows that your installation appears to be working correctly.
    "@
    

    笔记

    为了获得一致的体验,请使用正斜杠/作为路径,例如C:/而不是C:\moby/buildkit#4696中跟踪了对 Windows 样式路径的支持 。

  10. 构建映像并将其推送到注册表。

    > docker buildx build --push -t <username>/hello-buildkit .
    
  11. 推送到注册表后,使用 运行该映像docker run

    > docker run <username>/hello-world