BuildKit

概覽

BuildKit 是一個用於取代舊版建構器(legacy builder)的改進後端。對於 Docker Desktop 和 23.0 版之後的 Docker Engine 使用者而言,BuildKit 是預設的建構器。

BuildKit 提供了新功能並提升了建構效能。它也引入了對處理更複雜場景的支援:

  • 偵測並跳過執行未使用的建構階段
  • 平行處理獨立的建構階段
  • 在建構之間,僅以增量方式傳輸您 建構內容 (build context) 中已變更的檔案
  • 偵測並跳過傳輸 建構內容 中未使用的檔案
  • 使用包含許多新功能的 Dockerfile 前端 實作
  • 避免 API 其他部分(中間映像檔與容器)的副作用
  • 優先處理您的建構快取以進行自動清理

除了許多新功能外,BuildKit 主要改善了當前體驗的部分在於效能、儲存管理與擴充性。在效能方面,一項重大的更新是全新的完全並行建構圖求解器 (fully concurrent build graph solver)。它可以在可能時並行執行建構步驟,並優化掉對最終結果沒有影響的指令。存取本機原始檔案的方式也經過了優化。透過僅追蹤這些檔案在重複建構呼叫之間的更新,無需在工作開始前等待本機檔案被讀取或上傳。

LLB

BuildKit 的核心是 低階建構 (Low-Level Build, LLB) 定義格式。LLB 是一種中間二進位格式,允許開發人員擴充 BuildKit。LLB 定義了一個內容可定址的依賴關係圖,可用於組合複雜的建構定義。它還支援 Dockerfile 中未公開的功能,例如直接資料掛載和巢狀呼叫。

關於建構的執行與快取的一切都在 LLB 中定義。與舊版建構器相比,快取模型已完全重寫。LLB 不再使用啟發式方法來比較映像檔,而是直接追蹤建構圖和掛載到特定操作的內容校驗和。這使其速度更快、更精確且更具可移植性。建構快取甚至可以匯出到登錄檔,在那裡可以由任何主機上的後續呼叫按需提取。

LLB 可以直接使用 golang 用戶端套件 生成,該套件允許使用 Go 語言原語定義建構操作之間的關係。這賦予您執行任何您能想像到的事情的完整能力,但這可能不會是大多數人定義建構的方式。相反地,大多數使用者會使用前端元件或 LLB 巢狀呼叫來執行一組準備好的建構步驟。

前端 (Frontend)

前端是一個接收人類可讀建構格式並將其轉換為 LLB 以便 BuildKit 執行該格式的元件。前端可以作為映像檔分發,使用者可以針對特定版本的前端,保證其能適用於其定義所使用的功能。

例如,若要使用 BuildKit 建構 Dockerfile,您將會 使用外部 Dockerfile 前端

入門指南

對於 Docker Desktop 和 Docker Engine v23.0 及更新版本的使用者,BuildKit 是預設的建構器。

如果您已安裝 Docker Desktop,則無需啟用 BuildKit。如果您執行的 Docker Engine 版本低於 23.0,您可以透過設定環境變數,或將 BuildKit 設為常駐程式設定中的預設值來啟用它。

若要在執行 docker build 指令時設定 BuildKit 環境變數,請執行

$ 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 容器支援目前為實驗性質。

BuildKit 自 0.13 版起對 Windows 容器 (WCOW) 提供實驗性支援。本節將引導您完成嘗試的步驟。若要分享意見回饋,請 在儲存庫中開啟 issue,特別是關於 buildkitd.exe 的部分。

已知限制

有關 Windows 上 BuildKit 的已知錯誤與限制資訊,請參閱 GitHub issues

先決條件

  • 架構:amd64arm64(二進位檔可用,但尚未正式測試)。
  • 支援的作業系統:Windows Server 2019、Windows Server 2022、Windows 11。
  • 基礎映像檔:ServerCore:ltsc2019ServerCore:ltsc2022NanoServer:ltsc2022。請參閱 此處的相容性對照表
  • Docker Desktop 4.29 或更新版本

步驟

注意

以下指令需要在 PowerShell 終端機中具有管理員(提升)權限。

  1. 啟用 Hyper-VContainers Windows 功能。

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

    如果您看到 RestartNeededTrue,請重新啟動電腦並以管理員身分重新開啟 PowerShell 終端機。否則,請繼續執行下一個步驟。

  2. 在 Docker Desktop 中切換至 Windows 容器。

    選取工作列中的 Docker 圖示,然後選擇 Switch to Windows containers...

  3. 依照 此處 的設定說明安裝 containerd 1.7.7 或更新版本。

  4. 下載並解壓縮最新的 BuildKit 版本。

    $version = "v0.22.0" # 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. 將 BuildKit 二進位檔安裝至 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. 啟動 BuildKit 常駐程式。

    > buildkitd.exe
    
    注意

    如果您正在執行 dockerd 管理的 containerd 程序,請改用該程序,並提供位址:buildkitd.exe --containerd-worker-addr "npipe:////./pipe/docker-containerd"

  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
    

    輸出應顯示建構器平台為 Windows,且建構器的端點為具名管道 (named pipe)。

    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-buildkit 映像檔。

    > 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.
    "@
    
  10. 建構並推送該映像檔至登錄檔。

    > docker buildx build --push -t <username>/hello-buildkit .
    
  11. 推送至登錄檔後,使用 docker run 執行該映像檔。

    > docker run <username>/hello-buildkit
    
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.