Skip to content

Commit 1aaf6a9

Browse files
authored
Merge pull request #928 from ktock/build-doc
Add a document about setting up `nerdctl build` with BuildKit
2 parents 80263f7 + fcdaa2e commit 1aaf6a9

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Binaries are available here: https://github.com/containerd/nerdctl/releases
100100
In addition to containerd, the following components should be installed:
101101
- [CNI plugins](https://github.com/containernetworking/plugins): for using `nerdctl run`.
102102
- v1.1.0 or later is highly recommended. Older versions require extra [CNI isolation plugin](https://github.com/AkihiroSuda/cni-isolation) for isolating bridge networks (`nerdctl network create`).
103-
- [BuildKit](https://github.com/moby/buildkit) (OPTIONAL): for using `nerdctl build`. BuildKit daemon (`buildkitd`) needs to be running.
103+
- [BuildKit](https://github.com/moby/buildkit) (OPTIONAL): for using `nerdctl build`. BuildKit daemon (`buildkitd`) needs to be running. See also [the document about setting up BuildKit](./docs/build.md).
104104
- [RootlessKit](https://github.com/rootless-containers/rootlesskit) and [slirp4netns](https://github.com/rootless-containers/slirp4netns) (OPTIONAL): for [Rootless mode](./docs/rootless.md)
105105
- RootlessKit needs to be v0.10.0 or later. v0.14.1 or later is recommended.
106106
- slirp4netns needs to be v0.4.0 or later. v1.1.7 or later is recommended.
@@ -749,7 +749,7 @@ Usage: `nerdctl unpause CONTAINER [CONTAINER...]`
749749
### :whale: nerdctl build
750750
Build an image from a Dockerfile.
751751

752-
:information_source: Needs buildkitd to be running.
752+
:information_source: Needs buildkitd to be running. See also [the document about setting up `nerdctl build` with BuildKit](./docs/build.md).
753753

754754
Usage: `nerdctl build [OPTIONS] PATH`
755755

@@ -1459,6 +1459,7 @@ Basic features:
14591459
- [`./docs/compose.md`](./docs/compose.md): Compose
14601460
- [`./docs/rootless.md`](./docs/rootless.md): Rootless mode
14611461
- [`./docs/cni.md`](./docs/cni.md): CNI for containers network
1462+
- [`./docs/build.md`](./docs/build.md): `nerdctl build` with BuildKit
14621463

14631464
Advanced features:
14641465
- [`./docs/stargz.md`](./docs/stargz.md): Lazy-pulling using Stargz Snapshotter

docs/build.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Setting up `nerdctl build` with BuildKit
2+
3+
`nerdctl build` (and `nerdctl compose build`) relies on [BuildKit](https://github.com/moby/buildkit).
4+
To use it, you need to set up BuildKit.
5+
6+
BuildKit has 2 types of backends.
7+
8+
- **containerd worker**: BuildKit relies on containerd to manage containers and images, etc. containerd needs to be up-and-running on the host.
9+
- **OCI worker**: BuildKit manages containers and images, etc. containerd isn't needed. This worker relies on runc for container execution.
10+
11+
You need to set up BuildKit with either of the above workers.
12+
13+
Note that OCI worker cannot access base images (`FROM` images in Dockerfiles) managed by containerd.
14+
Thus you cannot let `nerdctl build` use containerd-managed images as the base image.
15+
They include images previously built using `nerdctl build`.
16+
17+
For example, the following build `bar` fails with OCI worker because it tries to use the previously built and containerd-managed image `foo`.
18+
19+
```console
20+
$ mkdir -p /tmp/ctx && cat <<EOF > /tmp/ctx/Dockerfile
21+
FROM ghcr.io/stargz-containers/ubuntu:20.04-org
22+
RUN echo hello
23+
EOF
24+
$ nerdctl build -t foo /tmp/ctx
25+
$ cat <<EOF > /tmp/ctx/Dockerfile
26+
FROM foo
27+
RUN echo bar
28+
EOF
29+
$ nerdctl build -t bar /tmp/ctx
30+
```
31+
32+
This limitation can be avoided using containerd worker as mentioned later.
33+
34+
## Setting up BuildKit with containerd worker
35+
36+
### Rootless (requires BuildKit v0.10.0 or later)
37+
38+
```
39+
$ CONTAINERD_NAMESPACE=default containerd-rootless-setuptool.sh install-buildkit-containerd
40+
```
41+
42+
`containerd-rootless-setuptool.sh` is aware of `CONTAINERD_NAMESPACE` and `CONTAINERD_SNAPSHOTTER` envvars.
43+
It installs buildkitd to the specified containerd namespace.
44+
This allows BuildKit using containerd-managed images in that namespace as the base image.
45+
Note that BuildKit can't use images in other namespaces as of now.
46+
47+
If `CONTAINERD_NAMESPACE` envvar is not specified, this script configures buildkitd to use "buildkit" namespace (not "default" namespace).
48+
49+
You can install an additional buildkitd process in a different namespace by executing this script with specifying the namespace with `CONTAINERD_NAMESPACE`.
50+
51+
BuildKit will expose the socket at `$XDG_RUNTIME_DIR/buildkit-$CONTAINERD_NAMESPACE/buildkitd.sock` if `CONTAINERD_NAMESPACE` is specified.
52+
If `CONTAINERD_NAMESPACE` is not specified, that location will be `$XDG_RUNTIME_DIR/buildkit/buildkitd.sock`.
53+
54+
### Rootful
55+
56+
```
57+
$ sudo systemctl enable --now buildkit
58+
```
59+
60+
Then add the following configuration to `/etc/buildkit/buildkitd.toml` to enable containerd worker.
61+
62+
```toml
63+
[worker.oci]
64+
enabled = false
65+
66+
[worker.containerd]
67+
enabled = true
68+
# namespace should be "k8s.io" for Kubernetes (including Rancher Desktop)
69+
namespace = "default"
70+
```
71+
72+
## Setting up BuildKit with OCI worker
73+
74+
### Rootless
75+
76+
```
77+
$ containerd-rootless-setuptool.sh install-buildkit
78+
```
79+
80+
As mentioned in the above, BuildKit with this configuration cannot use images managed by containerd.
81+
They include images previously built with `nerdctl build`.
82+
83+
BuildKit will expose the socket at `$XDG_RUNTIME_DIR/buildkit/buildkitd.sock`.
84+
85+
### rootful
86+
87+
```
88+
$ sudo systemctl enable --now buildkit
89+
```
90+
91+
## Which BuildKit socket will nerdctl use?
92+
93+
You can specify BuildKit address for `nerdctl build` using `--buildkit-host` flag or `BUILDKIT_HOST` envvar.
94+
When BuildKit address isn't specified, nerdctl tries some default BuildKit addresses the following order and uses the first available one.
95+
96+
- `<runtime directory>/buildkit-<current namespace>/buildkitd.sock`
97+
- `<runtime directory>/buildkit-default/buildkitd.sock`
98+
- `<runtime directory>/buildkit/buildkitd.sock`
99+
100+
For example, if you run rootless nerdctl with `test` containerd namespace, it tries to use `$XDG_RUNTIME_DIR/buildkit-test/buildkitd.sock` by default then try to fall back to `$XDG_RUNTIME_DIR/buildkit-default/buildkitd.sock` and `$XDG_RUNTIME_DIR/buildkit/buildkitd.sock`

0 commit comments

Comments
 (0)