Copyright(c)2022 NTT Corp.All Rights Reserved. 8
側⾯1︓Dockerの⼀部(docker build)
l `docker build`の実装(since 18.09)
l 環境変数`DOCKER_BUILDKIT=1`を指定することでBuildKitを有効化できる
l dockerd にBuildKitが直接統合されている
dockerd
BuildKit
docker build
Docker API
9.
Copyright(c)2022 NTT Corp.All Rights Reserved. 9
側⾯2︓汎⽤Dockerfileビルダ
l `buildkitd`というデーモンを使ってDockerfileのビルドが可能(gRPC APIを提供)。 buildkitd
のクライアントとして動作するビルダがある
• Docker Buildx(後述) https://github.com/docker/buildx
• nerdctl: Docker互換のcontainerd CLI https://github.com/containerd/nerdctl
l buildkitが提供するライブラリを使ってbuildkitdをアプリケーションに埋め込むことも可能
• e.g. img https://github.com/genuinetools/img
• Jess Frazelle (Oxide Computer)が開発。daemonless, rootlessなイメージビルダ
img
buildkitd
docker buildx
gRPC API
nerdctl build
buildctl
gRPC API
BuildKit
CLI
Copyright(c)2022 NTT Corp.All Rights Reserved. 13
並列マルチステージビルド
l 各ステージが並列に実⾏されることで旧`docker build`より⾼速化されている
l 旧`docker build`ではシーケンシャルな実⾏だった
stage C
FROM image AS stage-a
RUN build a
FROM image AS stage-b
RUN build b
FROM scratch
COPY --from=stage-a a /
COPY --from=stage-b b /
stage A stage B
14.
Copyright(c)2022 NTT Corp.All Rights Reserved. 14
Dockerfileの便利構⽂
l type=bind: context中のディレクトリや他のステージなどからマウント可能
RUN --mount
l type=cache: コンパイラなどで使うキャッシュをビルド間で共有できる
l type=secret: 秘密鍵などsecretをイメージに書き込むことなく使⽤可能
l type=ssh: SSH agent経由でSSH keyにアクセスできる
RUN --mount=type=cache,target=/root/.cache/go-build go build …
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials aws s3 cp s3://… …
RUN --mount=type=ssh ssh –q –T [email protected]
docker/dockerfile:1.2
https://github.com/moby/buildkit/blob/v0.10.3/frontend/dockerfile/docs/syntax.md
15.
Copyright(c)2022 NTT Corp.All Rights Reserved. 15
Dockerfileの便利構⽂
l RUN、COPY命令の引数にエスケープ⽂字無しに複数⾏にわたる命令を書ける
l shebangやset命令が使える
heredoc
FROM ubuntu
RUN <<EOT
set -eu -o pipefail
mkdir -p /foo/bar
echo hello > /foo/bar/hello
EOT
FROM python
RUN <<EOT
#!/usr/bin/env python
print(“hello world”)
EOT
docker/dockerfile:1.4
FROM ubuntu
COPY <<EOT /foo
foo
bar
EOT
https://github.com/moby/buildkit/blob/v0.10.3/frontend/dockerfile/docs/syntax.md
16.
Copyright(c)2022 NTT Corp.All Rights Reserved. 16
Dockerfileの便利構⽂
l キャッシュミスがしにくくなるよう最適化が施された
l --link=falseと異なり、COPY先のsymlinkが辿られないという違いに注意
COPY --link docker/dockerfile:1.4
https://github.com/moby/buildkit/blob/v0.10.3/frontend/dockerfile/docs/syntax.md
FROM ubuntu:22.04 AS dev
RUN echo foo > /foo
FROM busybox:1.34
COPY --link --from=dev /foo /
FROM ubuntu:22.04 AS dev
RUN echo foo > /foo
FROM alpine:3.16
COPY --link --from=dev /foo /
キャッシュを使いまわせる
17.
Copyright(c)2022 NTT Corp.All Rights Reserved. 17
マルチプラットフォームイメージ
l ⼀つのイメージに複数のプラットフォーム向けのイメージを詰め込める
l ランタイムは⾃分が稼働するホストのプラットフォームに合うものを選んでpullする
l Docker Buildxでビルド可能[1]: https://medium.com/nttlabs/buildx-multiarch-2c6c2df00ca2
• QEMU使⽤︓簡単(Dockerfileに変更不要)だがパフォーマンスペナルティあり
• Dockerfile内でクロスコンパイラ等使⽤︓早い。Dockerfileに変更が必要
• ターゲットアーキテクチャのマシンを⽤意してSSH経由でビルド︓早い。マシンを別
途⽤意する必要がある
docker buildx build –t my/image –push ¥
--platfom linux/amd64,linux/arm64 .
docker run my/image
Dockerfile
amd64⽤イメージ
arm⽤イメージ
・・・
amd64マシン
armマシン
マルチプラットフォー
ム対応イメージ
pull
pull
[1] Akihiro Suda. “Preparation toward running Docker on ARM Mac: Building multi-arch images with Docker BuildX”. https://medium.com/nttlabs/buildx-multiarch-2c6c2df00ca2
18.
Copyright(c)2022 NTT Corp.All Rights Reserved. 18
リモートキャッシュ
l ビルド結果はBuildKitを実⾏しているホスト上だけでなく、レジストリなどリモートへの
キャッシュ保持が可能
l Kubernetesでの分散ビルドやCIジョブなど、ノード間でキャッシュを共有するのに使える
l レジストリ以外にもGitHub ActionsやS3へのキャッシュ格納もexperimentalにサポート
local registry
inline
BuildKit
cache
ローカルなディレクトリ
にキャッシュ保持
キャッシュ込みのイメー
ジをレジストリにpush
BuildKit
image
+ cache
BuildKit
image cache
イメージとキャッシュを
別々にレジストリにpush
19.
Copyright(c)2022 NTT Corp.All Rights Reserved. 19
LLB︓ビルド定義の中間表現
LLB is to Dockerfile what LLVM IR is to C.
https://github.com/moby/buildkit#exploring-llb
l LLBにさえ変換できれば、任意の⾔語で記述したビルドをBulidKitで実⾏可能
l ビルド⼿順を定義したDAGを表現するデータ構造(protocol buffers)
FROM busybox AS foo
RUN echo foo > /foo
FROM alpine AS bar
RUN echo bar > /bar
FROM scratch
COPY --from=foo /foo /
COPY --from=bar /bar /
LLB
Dockerfile
20.
Copyright(c)2022 NTT Corp.All Rights Reserved. 20
さまざまなBuildKit対応⾔語(⼀部)
Earthly (Earthfile): https://github.com/earthly/earthly
l Earthly TechnologiesによるCI/CDフレームワーク
l MakefileとDockerfileを合わせたような記法でCI/CDのロジックが書ける
Dagger: https://github.com/dagger/dagger
l DaggerによるCI/CDフレームワーク
l cue⾔語でCI/CDロジックが書ける
buildkit-nix: https://github.com/AkihiroSuda/buildkit-nix
l Akihiro Sudaさん(mobyメンテナ, BuildKitメンテナ)が開発
l Nix derivationをDockerfileとして⽤い、イメージをビルドする
対応⾔語のリスト︓https://github.com/moby/buildkit#exploring-llb
• 2022/8/18時点で11個リストされている
• YAMLでビルドを記述するものや、RustやPythonなど特定⾔語のアプリケーションのビル
ドに使えるものなどいろいろある
Copyright(c)2022 NTT Corp.All Rights Reserved. 25
BuildKitサーバ
l ビルドに関するほとんどのことが⾏われる
• ビルド定義のパース
• ビルド、キャッシュ
• ビルド結果のエクスポート, etc…
l gRPC経由で操作⽤のControl APIを公開
• ローカルのunixソケット経由
• リモートのクライアントへの公開も可能
l ビルド定義⾔語をLLBに変換するコンポーネントがプラ
ガブル
• BuildKitにパッチを当てることなく任意の⾔語のサ
ポートが可能
クライアント
サーバ
(buildkitd)
gRPC API
26.
Copyright(c)2022 NTT Corp.All Rights Reserved. 26
BuildKitサーバを構成する主要なコンポーネント
l frontend
• ソース⾔語のパースやLLBへの変換
• プラグインとして拡張可能
l solver
• LLBを実⾏する
• 並列実⾏やキャッシュの利⽤によりビルドを⾼速化
l worker
• コンテナやキャッシュ、snapshot、レイヤコンテンツ、
ビルド結果のエクスポートなどを管理
• runcベースの実装とcontainerdベースの実装がある
• CNI pluginも利⽤可能
クライアント
gRPC API
frontend
solver
worker
サーバ(buildkitd)
27.
Copyright(c)2022 NTT Corp.All Rights Reserved. 27
BuildKit frontendによる任意のビルド⾔語の定義
l ソース⾔語をLLBに変換するコンポーネント
l コンテナとして実装し、BuildKitにプラグイン可能
l フロントエンドコンテナにはstdioを通じてGateway APIと
よばれるgRPC APIが提供される
• このAPI経由でビルドコンテキストを取得
• LLBを作成しその実⾏をこのAPI経由でBuildKitに指⽰
クライアント
gRPC API
frontend
Gateway API提供
(stdio経由)
l Gateway APIはクライアント向けAPIとしても提供される
l クライアントから⾒ると、buildkitd操作⽤API(Control
API)とGateway APIの2つが⾒える
l クライアントにフロントエンド的機能を実装可能
補⾜
サーバ(buildkitd)
コンテナ