|
| 1 | +# CDI |
| 2 | + |
| 3 | +[CDI (Container Device Interface)](https://github.com/cncf-tags/container-device-interface/blob/main/SPEC.md) |
| 4 | +provides a standard mechanism for device vendors to describe what is required |
| 5 | +to provide access to a specific resource such as a GPU beyond a simple device |
| 6 | +name. |
| 7 | + |
| 8 | +Since BuildKit 0.20.0, you can access devices using the CDI interface. This |
| 9 | +allows you to use devices like GPUs in your builds. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +To use CDI with BuildKit, you need to create the [CDI configuration file](https://github.com/cncf-tags/container-device-interface/blob/main/SPEC.md#cdi-json-specification) |
| 14 | +for your device (either JSON or YAML) and put it in one of the following |
| 15 | +locations: |
| 16 | +* `/etc/cdi` |
| 17 | +* `/var/run/cdi` |
| 18 | +* `/etc/buildkit/cdi` |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> Location can be changed by setting the `specDirs` option in the `cdi` section |
| 22 | +> of the [`buildkitd.toml` configuration file](buildkitd.toml.md). |
| 23 | +
|
| 24 | +Let's create a simple CDI configuration file that injects an environment |
| 25 | +variable into the build environment and write it to `/etc/cdi/foo.yml`: |
| 26 | + |
| 27 | +```yaml |
| 28 | +cdiVersion: "0.6.0" |
| 29 | +kind: "vendor1.com/device" |
| 30 | +devices: |
| 31 | +- name: foo |
| 32 | + containerEdits: |
| 33 | + env: |
| 34 | + - FOO=injected |
| 35 | +``` |
| 36 | +
|
| 37 | +Start BuildKit and check the list of available devices for the default worker: |
| 38 | +
|
| 39 | +```bash |
| 40 | +buildctl debug workers -v |
| 41 | + |
| 42 | +Platforms: linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6 |
| 43 | +BuildKit: github.com/moby/buildkit v0.19.0-rc3-72-gb89ee491b.m b89ee491bc1c40b8533f098dab18e414c8b70885.m |
| 44 | +Labels: |
| 45 | + org.mobyproject.buildkit.worker.executor: oci |
| 46 | + org.mobyproject.buildkit.worker.hostname: cc39352c87dd |
| 47 | + org.mobyproject.buildkit.worker.network: host |
| 48 | + org.mobyproject.buildkit.worker.oci.process-mode: sandbox |
| 49 | + org.mobyproject.buildkit.worker.selinux.enabled: false |
| 50 | + org.mobyproject.buildkit.worker.snapshotter: overlayfs |
| 51 | +Devices: |
| 52 | + Name: vendor1.com/device=foo |
| 53 | + AutoAllow: true |
| 54 | + |
| 55 | +GC Policy rule#0: |
| 56 | + All: false |
| 57 | + Filters: type==source.local,type==exec.cachemount,type==source.git.checkout |
| 58 | + Keep duration: 48h0m0s |
| 59 | + Maximum used space: 512MB |
| 60 | +GC Policy rule#1: |
| 61 | + All: false |
| 62 | + Keep duration: 1440h0m0s |
| 63 | + Reserved space: 10GB |
| 64 | + Minimum free space: 202GB |
| 65 | + Maximum used space: 100GB |
| 66 | +GC Policy rule#2: |
| 67 | + All: false |
| 68 | + Reserved space: 10GB |
| 69 | + Minimum free space: 202GB |
| 70 | + Maximum used space: 100GB |
| 71 | +GC Policy rule#3: |
| 72 | + All: true |
| 73 | + Reserved space: 10GB |
| 74 | + Minimum free space: 202GB |
| 75 | + Maximum used space: 100GB |
| 76 | +``` |
| 77 | +
|
| 78 | +Now let's create a Dockerfile to use this device: |
| 79 | +
|
| 80 | +```dockerfile |
| 81 | +FROM busybox |
| 82 | +RUN --device=vendor1.com/device=foo \ |
| 83 | + env|grep FOO |
| 84 | +``` |
| 85 | + |
| 86 | +And check if the environment variable is injected during build: |
| 87 | + |
| 88 | +```bash |
| 89 | +buildctl build --frontend=dockerfile.v0 --no-cache --local context=. --local dockerfile=. |
| 90 | +#1 [internal] load build definition from Dockerfile |
| 91 | +#1 transferring dockerfile: 99B done |
| 92 | +#1 DONE 0.0s |
| 93 | + |
| 94 | +#2 [internal] load metadata for docker.io/library/busybox:latest |
| 95 | +#2 ... |
| 96 | + |
| 97 | +#3 [auth] library/busybox:pull token for registry-1.docker.io |
| 98 | +#3 DONE 0.0s |
| 99 | + |
| 100 | +#2 [internal] load metadata for docker.io/library/busybox:latest |
| 101 | +#2 DONE 0.8s |
| 102 | + |
| 103 | +#4 [internal] load .dockerignore |
| 104 | +#4 transferring context: 2B done |
| 105 | +#4 DONE 0.0s |
| 106 | + |
| 107 | +#5 [1/2] FROM docker.io/library/busybox:latest@sha256:a5d0ce49aa801d475da48f8cb163c354ab95cab073cd3c138bd458fc8257fbf1 |
| 108 | +#5 resolve docker.io/library/busybox:latest@sha256:a5d0ce49aa801d475da48f8cb163c354ab95cab073cd3c138bd458fc8257fbf1 0.0s done |
| 109 | +#5 CACHED |
| 110 | + |
| 111 | +#6 [2/2] RUN --device=vendor1.com/device=foo env|grep FOO |
| 112 | +#6 0.062 FOO=injected |
| 113 | +#6 DONE 0.1s |
| 114 | +``` |
0 commit comments