BuildKit

概觀

BuildKit環境中已變更的檔案

  • 偵測並跳過傳輸建置環境中未使用的檔案
  • 使用具有許多新功能的 Dockerfile 前端 實作
  • 避免與其餘 API(中間映像檔和容器)產生副作用
  • 將建置快取設為優先順序以進行自動修剪
  • 除了許多新功能之外,BuildKit 主要改進的領域是效能、儲存管理和擴充性。在效能方面,一個重大的更新是全新的完全並行程式建置圖形解題器。它可以在可能的情況下平行執行建置步驟,並最佳化對最終結果沒有影響的指令。我們還最佳化了對本機原始檔案的存取。透過僅追蹤在重複建置呼叫之間對這些檔案進行的更新,無需等待讀取或上傳本機檔案即可開始工作。

    LLB

    BuildKit 的核心是 低階建置 (LLB)

    關於建置的執行和快取的所有內容都在 LLB 中定義。與舊版建置器相比,快取模型已完全改寫。LLB 不是使用啟發式方法來比較映像檔,而是直接追蹤建置圖形的校驗和以及掛載到特定操作的內容。這使得它更快、更精確且更具可攜性。建置快取甚至可以匯出到 Registry,在任何主機上後續呼叫時可以按需提取。

    LLB 可以使用 golang 用戶端套件前端

    前端是一個元件,它採用人類可讀的建置格式並將其轉換為 LLB,以便 BuildKit 可以執行它。前端可以作為映像檔發佈,使用者可以鎖定特定版本的前端,以確保其適用於其定義中使用的功能。

    例如,要使用 BuildKit 建置 Dockerfile,您需要 使用外部 Dockerfile 前端

    入門

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

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

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

    $ DOCKER_BUILDKIT=1 docker build .
    

    備註

    Buildx 一律使用 BuildKit。

    若要預設使用 Docker BuildKit,請在 /etc/docker/daemon.json 中編輯 Docker daemon 設定如下,然後重新啟動 daemon。

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

    如果 /etc/docker/daemon.json 檔案不存在,請建立一個名為 daemon.json 的新檔案,然後將以下內容新增到檔案中。然後重新啟動 Docker daemon。

    Windows 上的 BuildKit

    警告

    BuildKit 僅完全支援建置 Linux 容器。Windows 容器支援仍處於實驗階段,並在 moby/buildkit#616 中追蹤。

    BuildKit 自 0.13 版起已實驗性地支援 Windows 容器 (WCOW)。本節將逐步引導您試用。我們感謝您透過 在此提交問題 提交的任何意見回饋,尤其是關於 buildkitd.exe 的部分。

    已知限制

    • Windows 上的 BuildKit 目前僅支援 containerd worker。非 OCI worker 的支援在 moby/buildkit#4836 中追蹤。

    先決條件

    • 架構:amd64arm64(二進制檔案可用,但尚未經過官方測試)。
    • 支援的作業系統:Windows Server 2019、Windows Server 2022、Windows 11。
    • 基礎映像檔:ServerCore:ltsc2019ServerCore:ltsc2022NanoServer:ltsc2022。請參閱 此處的相容性對應表
    • Docker Desktop 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 上安裝 BuildKit 二進制檔案。

      # 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 daemon。

      > buildkitd.exe
      
    7. 在另一個具有系統管理員權限的終端機中,建立一個使用本機 BuildKit daemon 的遠端 builder。

      備註

      這需要 Docker Desktop 4.29 版或更新版本。

      > docker buildx create --name buildkit-exp --use --driver=remote npipe:////./pipe/buildkitd
      buildkit-exp
      
    8. 透過執行 docker buildx inspect 來驗證 builder 連線。

      > docker buildx inspect
      

      輸出應指示 builder 平台為 Windows,且 builder 的端點為命名管道。

      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. 建置映像檔並推送至 registry。

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

      > docker run <username>/hello-buildkit