1. Prerequisite of using nerdctl build

buildctl needs to be installed and buildkitd needs to be running.

2. check if buildctl installed

$ nerdctl version

Client:
Version: v1.7.2
OS/Arch: linux/amd64
Git commit: e32c4b023bf41e5c8325cfb893a53cefb5fc68ed
buildctl:
Version: v0.12.4
GitCommit: 833949d0f7908608b00ab6b93b8f92bdb147fcca

Server:
containerd:
Version: v1.7.11
GitCommit: 64b8a811b07ba6288238eefc14d898ee0b5b99ba
runc:
Version: 1.1.10
GitCommit: v1.1.10-0-g18a0cb0f
(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$

If buildctl version shown in the output, buildctl has been installed.

3. check if buildkitd is running

systemctl status buildkitd

4. if buildkitd is not running, start it.

$ sudo systemctl daemon-reload
$ sudo systemctl start buildkitd
$ systemctl enable buildkitd

Created symlink /etc/systemd/system/multi-user.target.wants/buildkitd.service → /lib/systemd/system/buildkitd.service.

If Error: Unit xxx not found, this is due to there is no files buildkitd.service and buildkit.socket, create them:

$ sudo gedit /lib/systemd/system/buildkit.socket

# file /lib/systemd/system/buildkit.socket
[Unit]
Description=BuildKit
Documention=https://github.com/moby/buildkit

[Socket]
ListenStream=%t/buildkit/buildkitd.sock

[Install]
WantedBy=sockets.target

$ sudo gedit /lib/systemd/system/buildkitd.service 

# file /lib/systemd/system/buildkitd.service
[Unit]
Description=BuildKit
Require=buildkit.socket
After=buildkit.socket
Documention=https://github.com/moby/buildkit

[Service]
ExecStart=/usr/local/bin/buildkitd --oci-worker=false --containerd-worker=true

[Install]
WantedBy=multi-user.target

Then, start buildkitd again:

$ sudo systemctl daemon-reload
$ sudo systemctl start buildkitd
$ systemctl enable buildkitd

Created symlink /etc/systemd/system/multi-user.target.wants/buildkitd.service → /lib/systemd/system/buildkitd.service.

# check status of buildkitd
$ systemctl status buildkitd

● buildkitd.service - BuildKit
Loaded: loaded (/lib/systemd/system/buildkitd.service; enabled; vendor preset:>
Active: active (running) since Thu 2024-02-01 15:16:41 CST; 1min 35s ago
Main PID: 1505075 (buildkitd)
Tasks: 10 (limit: 14115)
Memory: 9.1M
CPU: 86ms
CGroup: /system.slice/buildkitd.service
└─1505075 /usr/local/bin/buildkitd --oci-worker=false --

5. use sudo nerdctl build to build container image from docker file

$ sudo nerdctl build --no-cache -t tf_std_server:v1 -f Dockerfile.tf_std_server .

(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$ sudo nerdctl build --no-cache -t tf_std_server:v1 -f Dockerfile.tf_std_server .
[+] Building 3741.3s (7/7) FINISHED
=> [internal] load build definition from Dockerfile.tf_std_server 0.9s
=> => transferring dockerfile: 158B 0.0s
=> [internal] load metadata for docker.io/tensorflow/tensorflow:nightly-gpu 6.0s
=> [internal] load .dockerignore 0.6s
=> => transferring context: 2B 0.0s
=> [internal] load build context 1.9s
=> => transferring context: 1.31kB 0.0s
=> [1/2] FROM docker.io/tensorflow/tensorflow:nightly-gpu@sha256:1b9d78ea53e00988bdec6b89213eb967a823e5eb2a59e34a7ddd2c3825b576ce 3132.2s
=> => resolve docker.io/tensorflow/tensorflow:nightly-gpu@sha256:1b9d78ea53e00988bdec6b89213eb967a823e5eb2a59e34a7ddd2c3825b576ce 1.1s
=> => sha256:e74c2cb8c2daf8e7f20d5c89e649b54d2c55ff6fe38c4d0698a14bd979546661 1.13kB / 1.13kB 4.1s
=> => sha256:6fa98202cc0d88496208a0fb5ca9bdf65c58b2a3df21e5882af7f1fa77ac4796 1.13kB / 1.13kB 4.1s
=> => sha256:09e20d668ae0282f2cf36cbd038ef657d3f620267292309ff0a71abb2e6362d9 6.90kB / 6.90kB 3.6s
=> => sha256:680a80bd788865a81a4b5753812be435249c2a85293fe9a6f0c5a293cb51282d 808B / 808B 3.9s
=> => sha256:c139eef115d10483c12436d45f5dadff21efe04a603e1792ddf16e2ea220320d 677.26MB / 677.26MB 284.0s
=> => sha256:a856c4f368bad9ccac8ddc08d73d5ebe5082898a014574420ca282c926a8dab2 37.29MB / 37.29MB 9.9s
=> => sha256:891cf252afe9c3f25eda3c50f6f4aaf4016077f22c4d78b7f87a8649ae510038 127B / 127B 0.8s
=> => sha256:f76680bdaf18ce22d0dc71b099ab8fe2147e37d261c9dc89fa29c6403a17f2f8 972B / 972B 1.2s
=> => sha256:97f009b3c181c6c5514409f3ef1058a8b04a7bf26cde998f566bb7539b557a02 2.98GB / 2.98GB 2504.7s
=> => sha256:3a6e10d7e4e9856303c630329324340a5591b82307737246695c61f798fc45f5 34.87MB / 34.87MB 9.9s
=> => sha256:0bbef35c2ff6cdf194eaecae34a64b8e9fb75694d8e502c7c88c390f8800d355 397B / 397B 0.6s
=> => sha256:5c851b555cd4eeb945c7fc1830dd86f14d070d4fd6ec879736b2df26b0871ecf 769B / 769B 1.2s
=> => sha256:82f3a5f8eeeeadbd27d8ba158b561036f5d49010507b2bf5ff7771d3002fa0db 861B / 861B 0.7s
=> => sha256:7d4f0f8effa7864e2b25cac47ecb80e71881841ce2b2a32d9e4c06d5696635b5 6.88kB / 6.88kB 0.8s
=> => sha256:89aa5c6f87943c439cf2ceb52a6b13506ea6a1e614d14570b407ae3dfeede332 0B / 185B 3705.3s
=> => sha256:d1937dd2edf2b94d5f600d1112c804645e3d345cb93bf15444910612534f1f41 57.07MB / 57.07MB 14.4s
=> => sha256:03bb9eb021f579ed871d5fee2344ba68d4509e9fc3b8865cf47fad506fa2f5d7 4.62MB / 4.62MB 2.2s
=> => sha256:aece8493d3972efa43bfd4ee3cdba659c0f787f8f59c82fb3e48c87cbb22a12e 29.54MB / 29.54MB 7.7s
=> => extracting sha256:aece8493d3972efa43bfd4ee3cdba659c0f787f8f59c82fb3e48c87cbb22a12e 3.6s
=> => extracting sha256:03bb9eb021f579ed871d5fee2344ba68d4509e9fc3b8865cf47fad506fa2f5d7 0.9s
=> => extracting sha256:d1937dd2edf2b94d5f600d1112c804645e3d345cb93bf15444910612534f1f41 3.1s
=> => extracting sha256:89aa5c6f87943c439cf2ceb52a6b13506ea6a1e614d14570b407ae3dfeede332 0.4s
=> => extracting sha256:7d4f0f8effa7864e2b25cac47ecb80e71881841ce2b2a32d9e4c06d5696635b5 0.3s
=> => extracting sha256:82f3a5f8eeeeadbd27d8ba158b561036f5d49010507b2bf5ff7771d3002fa0db 0.3s
=> => extracting sha256:5c851b555cd4eeb945c7fc1830dd86f14d070d4fd6ec879736b2df26b0871ecf 0.4s
=> => extracting sha256:0bbef35c2ff6cdf194eaecae34a64b8e9fb75694d8e502c7c88c390f8800d355 0.6s
=> => extracting sha256:3a6e10d7e4e9856303c630329324340a5591b82307737246695c61f798fc45f5 3.3s
=> => extracting sha256:97f009b3c181c6c5514409f3ef1058a8b04a7bf26cde998f566bb7539b557a02 415.0s
=> => extracting sha256:f76680bdaf18ce22d0dc71b099ab8fe2147e37d261c9dc89fa29c6403a17f2f8 1.4s
=> => extracting sha256:891cf252afe9c3f25eda3c50f6f4aaf4016077f22c4d78b7f87a8649ae510038 3.1s
=> => extracting sha256:a856c4f368bad9ccac8ddc08d73d5ebe5082898a014574420ca282c926a8dab2 50.5s
=> => extracting sha256:c139eef115d10483c12436d45f5dadff21efe04a603e1792ddf16e2ea220320d 136.8s
=> => extracting sha256:680a80bd788865a81a4b5753812be435249c2a85293fe9a6f0c5a293cb51282d 0.6s
=> => extracting sha256:09e20d668ae0282f2cf36cbd038ef657d3f620267292309ff0a71abb2e6362d9 1.9s
=> => extracting sha256:6fa98202cc0d88496208a0fb5ca9bdf65c58b2a3df21e5882af7f1fa77ac4796 0.6s
=> => extracting sha256:e74c2cb8c2daf8e7f20d5c89e649b54d2c55ff6fe38c4d0698a14bd979546661 0.6s
=> [2/2] COPY tf_std_server.py / 22.8s
=> exporting to docker image format 534.1s
=> => exporting layers 5.8s
=> => exporting manifest sha256:5364a028bff0588fcd3b9a91489b64ba59d17fc390ef516bdd11d77db257e916 0.3s
=> => exporting config sha256:d39144c35ea9a32641039358493137fdbce32ee5688b2c307cf255d127e6a0ed 0.4s
=> => sending tarball 527.0s
error: failed to solve: DeadlineExceeded: context deadline exceeded
unpacking docker.io/library/tf_std_server:v1 (sha256:5364a028bff0588fcd3b9a91489b64ba59d17fc390ef516bdd11d77db257e916)...
Loaded image: docker.io/library/tf_std_server:v1
(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$

Attention:
need 'sudo' to run the command as root, or:

(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$ nerdctl build --no-cache -t tf_std_server:v1 -f Dockerfile.tf_std_server .
ERRO[0000] buildctl needs to be installed and buildkitd needs to be running, see https://github.com/moby/buildkit , and containerd-rootless-setuptool.sh install-buildkit for OCI worker or containerd-rootless-setuptool.sh install-buildkit-containerd for containerd worker error="failed to ping to host unix:///run/user/1000/buildkit-default/buildkitd.sock: exit status 1\nfailed to ping to host unix:///run/user/1000/buildkit/buildkitd.sock: exit status 1"
FATA[0000] no buildkit host is available, tried 2 candidates: failed to ping to host unix:///run/user/1000/buildkit-default/buildkitd.sock: exit status 1
failed to ping to host unix:///run/user/1000/buildkit/buildkitd.sock: exit status 1

6. sudo nerdctl images to check the image

(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$ sudo nerdctl images

[sudo] password for maye:
REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE
tf_std_server v1 5364a028bff0 2 hours ago linux/amd64 7.0 GiB 3.6 GiB
tensorflow/serving latest fdc296e313fa 4 weeks ago linux/amd64 526.3 MiB 138.7 MiB
gcr.io/ml-pipeline/metadata-writer 2.0.1 e0e652bb8d5e 8 weeks ago linux/amd64 1.1 GiB 390.6 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/activator c2994c2b6c2c 8 weeks ago linux/amd64 56.6 MiB 16.8 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/autoscaler 8319aa662b49 8 weeks ago linux/amd64 57.1 MiB 17.0 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/controller 98a2cc7fd62e 8 weeks ago linux/amd64 62.6 MiB 18.7 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/domain-mapping-webhook 7368aaddf2be 8 weeks ago linux/amd64 52.5 MiB 16.2 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/domain-mapping f66c41ad7a73 8 weeks ago linux/amd64 54.1 MiB 16.6 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/queue dabaecec3886 8 weeks ago linux/amd64 33.3 MiB 10.3 MiB
gcr.lank8s.cn/knative-releases/knative.dev/serving/cmd/webhook 4305209ce498 8 weeks ago linux/amd64 55.9 MiB 16.6 MiB
gcr.lank8s.cn/ml-pipeline/metadata-writer 2.0.1 e0e652bb8d5e 8 weeks ago linux/amd64 1.1 GiB 390.6 MiB
m.daocloud.io/gcr.io/ml-pipeline/metadata-envoy 2.0.0-alpha.7 a3e53e33c517 8 weeks ago linux/amd64 251.0 MiB 98.9 MiB
(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$

Attention:
need 'sudo' to run as root, or the image built by nerdctl build not shown.