• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..25-Dec-2020-

api/H25-Dec-2020-7,5927,248

cache/H25-Dec-2020-7,7916,675

client/H25-Dec-2020-4,9734,194

cmd/buildkitd/config/H25-Dec-2020-173125

control/H25-Dec-2020-645559

executor/H25-Dec-2020-1,5201,259

exporter/H25-Dec-2020-383317

frontend/H25-Dec-2020-20,29618,061

identity/H25-Dec-2020-5423

session/H25-Dec-2020-7,6366,833

snapshot/H25-Dec-2020-474384

solver/H25-Dec-2020-21,59919,470

source/H25-Dec-2020-1,9581,684

util/H25-Dec-2020-6,2755,132

worker/H25-Dec-2020-325267

LICENSEH A D25-Dec-202011.1 KiB202169

README.mdH A D25-Dec-202019.3 KiB496363

go.modH A D25-Dec-20203.9 KiB8076

README.md

1[![asciicinema example](https://asciinema.org/a/gPEIEo1NzmDTUu2bEPsUboqmU.png)](https://asciinema.org/a/gPEIEo1NzmDTUu2bEPsUboqmU)
2
3# BuildKit
4
5[![GoDoc](https://godoc.org/github.com/moby/buildkit?status.svg)](https://godoc.org/github.com/moby/buildkit/client/llb)
6[![Build Status](https://github.com/moby/buildkit/workflows/build/badge.svg)](https://github.com/moby/buildkit/actions?query=workflow%3Abuild)
7[![Go Report Card](https://goreportcard.com/badge/github.com/moby/buildkit)](https://goreportcard.com/report/github.com/moby/buildkit)
8[![codecov](https://codecov.io/gh/moby/buildkit/branch/master/graph/badge.svg)](https://codecov.io/gh/moby/buildkit)
9
10BuildKit is a toolkit for converting source code to build artifacts in an efficient, expressive and repeatable manner.
11
12Key features:
13
14-   Automatic garbage collection
15-   Extendable frontend formats
16-   Concurrent dependency resolution
17-   Efficient instruction caching
18-   Build cache import/export
19-   Nested build job invocations
20-   Distributable workers
21-   Multiple output formats
22-   Pluggable architecture
23-   Execution without root privileges
24
25Read the proposal from https://github.com/moby/moby/issues/32925
26
27Introductory blog post https://blog.mobyproject.org/introducing-buildkit-17e056cc5317
28
29Join `#buildkit` channel on [Docker Community Slack](http://dockr.ly/slack)
30
31:information_source: If you are visiting this repo for the usage of BuildKit-only Dockerfile features like `RUN --mount=type=(bind|cache|tmpfs|secret|ssh)`, please refer to [`frontend/dockerfile/docs/syntax.md`](frontend/dockerfile/docs/syntax.md).
32
33:information_source: [BuildKit has been integrated to `docker build` since Docker 18.06 .](https://docs.docker.com/develop/develop-images/build_enhancements/)
34You don't need to read this document unless you want to use the full-featured standalone version of BuildKit.
35
36<!-- START doctoc generated TOC please keep comment here to allow auto update -->
37<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
38
39
40- [Used by](#used-by)
41- [Quick start](#quick-start)
42  - [Starting the `buildkitd` daemon:](#starting-the-buildkitd-daemon)
43  - [Exploring LLB](#exploring-llb)
44  - [Exploring Dockerfiles](#exploring-dockerfiles)
45    - [Building a Dockerfile with `buildctl`](#building-a-dockerfile-with-buildctl)
46    - [Building a Dockerfile using external frontend:](#building-a-dockerfile-using-external-frontend)
47    - [Building a Dockerfile with experimental features like `RUN --mount=type=(bind|cache|tmpfs|secret|ssh)`](#building-a-dockerfile-with-experimental-features-like-run---mounttypebindcachetmpfssecretssh)
48  - [Output](#output)
49    - [Image/Registry](#imageregistry)
50    - [Local directory](#local-directory)
51    - [Docker tarball](#docker-tarball)
52    - [OCI tarball](#oci-tarball)
53    - [containerd image store](#containerd-image-store)
54- [Cache](#cache)
55  - [Garbage collection](#garbage-collection)
56  - [Export cache](#export-cache)
57    - [Inline (push image and cache together)](#inline-push-image-and-cache-together)
58    - [Registry (push image and cache separately)](#registry-push-image-and-cache-separately)
59    - [Local directory](#local-directory-1)
60    - [`--export-cache` options](#--export-cache-options)
61    - [`--import-cache` options](#--import-cache-options)
62  - [Consistent hashing](#consistent-hashing)
63- [Expose BuildKit as a TCP service](#expose-buildkit-as-a-tcp-service)
64  - [Load balancing](#load-balancing)
65- [Containerizing BuildKit](#containerizing-buildkit)
66  - [Podman](#podman)
67  - [Kubernetes](#kubernetes)
68  - [Daemonless](#daemonless)
69- [Opentracing support](#opentracing-support)
70- [Running BuildKit without root privileges](#running-buildkit-without-root-privileges)
71- [Building multi-platform images](#building-multi-platform-images)
72- [Contributing](#contributing)
73
74<!-- END doctoc generated TOC please keep comment here to allow auto update -->
75
76## Used by
77
78BuildKit is used by the following projects:
79
80-   [Moby & Docker](https://github.com/moby/moby/pull/37151) (`DOCKER_BUILDKIT=1 docker build`)
81-   [img](https://github.com/genuinetools/img)
82-   [OpenFaaS Cloud](https://github.com/openfaas/openfaas-cloud)
83-   [container build interface](https://github.com/containerbuilding/cbi)
84-   [Tekton Pipelines](https://github.com/tektoncd/catalog) (formerly [Knative Build Templates](https://github.com/knative/build-templates))
85-   [the Sanic build tool](https://github.com/distributed-containers-inc/sanic)
86-   [vab](https://github.com/stellarproject/vab)
87-   [Rio](https://github.com/rancher/rio)
88-   [PouchContainer](https://github.com/alibaba/pouch)
89-   [Docker buildx](https://github.com/docker/buildx)
90-   [Okteto Cloud](https://okteto.com/)
91-   [Earthly earthfiles](https://github.com/vladaionescu/earthly)
92
93## Quick start
94
95:information_source: For Kubernetes deployments, see [`examples/kubernetes`](./examples/kubernetes).
96
97BuildKit is composed of the `buildkitd` daemon and the `buildctl` client.
98While the `buildctl` client is available for Linux, macOS, and Windows, the `buildkitd` daemon is only available for Linux currently.
99
100The `buildkitd` daemon requires the following components to be installed:
101-   [runc](https://github.com/opencontainers/runc) or [crun](https://github.com/containers/crun)
102-   [containerd](https://github.com/containerd/containerd) (if you want to use containerd worker)
103
104The latest binaries of BuildKit are available [here](https://github.com/moby/buildkit/releases) for Linux, macOS, and Windows.
105
106[Homebrew package](https://formulae.brew.sh/formula/buildkit) (unofficial) is available for macOS.
107```console
108$ brew install buildkit
109```
110
111To build BuildKit from source, see [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md).
112
113### Starting the `buildkitd` daemon:
114
115You need to run `buildkitd` as the root user on the host.
116
117```bash
118$ sudo buildkitd
119```
120
121To run `buildkitd` as a non-root user, see [`docs/rootless.md`](docs/rootless.md).
122
123The buildkitd daemon supports two worker backends: OCI (runc) and containerd.
124
125By default, the OCI (runc) worker is used. You can set `--oci-worker=false --containerd-worker=true` to use the containerd worker.
126
127We are open to adding more backends.
128
129The buildkitd daemon listens gRPC API on `/run/buildkit/buildkitd.sock` by default, but you can also use TCP sockets.
130See [Expose BuildKit as a TCP service](#expose-buildkit-as-a-tcp-service).
131
132### Exploring LLB
133
134BuildKit builds are based on a binary intermediate format called LLB that is used for defining the dependency graph for processes running part of your build. tl;dr: LLB is to Dockerfile what LLVM IR is to C.
135
136-   Marshaled as Protobuf messages
137-   Concurrently executable
138-   Efficiently cacheable
139-   Vendor-neutral (i.e. non-Dockerfile languages can be easily implemented)
140
141See [`solver/pb/ops.proto`](./solver/pb/ops.proto) for the format definition, and see [`./examples/README.md`](./examples/README.md) for example LLB applications.
142
143Currently, the following high-level languages has been implemented for LLB:
144
145-   Dockerfile (See [Exploring Dockerfiles](#exploring-dockerfiles))
146-   [Buildpacks](https://github.com/tonistiigi/buildkit-pack)
147-   [Mockerfile](https://matt-rickard.com/building-a-new-dockerfile-frontend/)
148-   [Gockerfile](https://github.com/po3rin/gockerfile)
149-   [bldr (Pkgfile)](https://github.com/talos-systems/bldr/)
150-   [HLB](https://github.com/openllb/hlb)
151-   [Earthfile (Earthly)](https://github.com/earthly/earthly)
152-   [Cargo Wharf (Rust)](https://github.com/denzp/cargo-wharf)
153-   (open a PR to add your own language)
154
155### Exploring Dockerfiles
156
157Frontends are components that run inside BuildKit and convert any build definition to LLB. There is a special frontend called gateway (`gateway.v0`) that allows using any image as a frontend.
158
159During development, Dockerfile frontend (`dockerfile.v0`) is also part of the BuildKit repo. In the future, this will be moved out, and Dockerfiles can be built using an external image.
160
161#### Building a Dockerfile with `buildctl`
162
163```bash
164buildctl build \
165    --frontend=dockerfile.v0 \
166    --local context=. \
167    --local dockerfile=.
168# or
169buildctl build \
170    --frontend=dockerfile.v0 \
171    --local context=. \
172    --local dockerfile=. \
173    --opt target=foo \
174    --opt build-arg:foo=bar
175```
176
177`--local` exposes local source files from client to the builder. `context` and `dockerfile` are the names Dockerfile frontend looks for build context and Dockerfile location.
178
179#### Building a Dockerfile using external frontend:
180
181External versions of the Dockerfile frontend are pushed to https://hub.docker.com/r/docker/dockerfile-upstream and https://hub.docker.com/r/docker/dockerfile and can be used with the gateway frontend. The source for the external frontend is currently located in `./frontend/dockerfile/cmd/dockerfile-frontend` but will move out of this repository in the future ([#163](https://github.com/moby/buildkit/issues/163)). For automatic build from master branch of this repository `docker/dockerfile-upstream:master` or `docker/dockerfile-upstream:master-labs` image can be used.
182
183```bash
184buildctl build \
185    --frontend gateway.v0 \
186    --opt source=docker/dockerfile \
187    --local context=. \
188    --local dockerfile=.
189buildctl build \
190    --frontend gateway.v0 \
191    --opt source=docker/dockerfile \
192    --opt context=git://github.com/moby/moby \
193    --opt build-arg:APT_MIRROR=cdn-fastly.deb.debian.org
194```
195
196#### Building a Dockerfile with experimental features like `RUN --mount=type=(bind|cache|tmpfs|secret|ssh)`
197
198See [`frontend/dockerfile/docs/experimental.md`](frontend/dockerfile/docs/experimental.md).
199
200### Output
201
202By default, the build result and intermediate cache will only remain internally in BuildKit. An output needs to be specified to retrieve the result.
203
204#### Image/Registry
205
206```bash
207buildctl build ... --output type=image,name=docker.io/username/image,push=true
208```
209
210To export the cache embed with the image and pushing them to registry together, type `registry` is required to import the cache, you should specify `--export-cache type=inline` and `--import-cache type=registry,ref=...`. To export the cache to a local directy, you should specify `--export-cache type=local`.
211Details in [Export cache](#export-cache).
212
213```bash
214buildctl build ...\
215  --output type=image,name=docker.io/username/image,push=true \
216  --export-cache type=inline \
217  --import-cache type=registry,ref=docker.io/username/image
218```
219
220Keys supported by image output:
221* `name=[value]`: image name
222* `push=true`: push after creating the image
223* `push-by-digest=true`: push unnamed image
224* `registry.insecure=true`: push to insecure HTTP registry
225* `oci-mediatypes=true`: use OCI mediatypes in configuration JSON instead of Docker's
226* `unpack=true`: unpack image after creation (for use with containerd)
227* `dangling-name-prefix=[value]`: name image with `prefix@<digest>` , used for anonymous images
228* `name-canonical=true`: add additional canonical name `name@<digest>`
229* `compression=[uncompressed,gzip]`: choose compression type for layer, gzip is default value
230
231
232If credentials are required, `buildctl` will attempt to read Docker configuration file `$DOCKER_CONFIG/config.json`.
233`$DOCKER_CONFIG` defaults to `~/.docker`.
234
235#### Local directory
236
237The local client will copy the files directly to the client. This is useful if BuildKit is being used for building something else than container images.
238
239```bash
240buildctl build ... --output type=local,dest=path/to/output-dir
241```
242
243To export specific files use multi-stage builds with a scratch stage and copy the needed files into that stage with `COPY --from`.
244
245```dockerfile
246...
247FROM scratch as testresult
248
249COPY --from=builder /usr/src/app/testresult.xml .
250...
251```
252
253```bash
254buildctl build ... --opt target=testresult --output type=local,dest=path/to/output-dir
255```
256
257Tar exporter is similar to local exporter but transfers the files through a tarball.
258
259```bash
260buildctl build ... --output type=tar,dest=out.tar
261buildctl build ... --output type=tar > out.tar
262```
263
264#### Docker tarball
265
266```bash
267# exported tarball is also compatible with OCI spec
268buildctl build ... --output type=docker,name=myimage | docker load
269```
270
271#### OCI tarball
272
273```bash
274buildctl build ... --output type=oci,dest=path/to/output.tar
275buildctl build ... --output type=oci > output.tar
276```
277#### containerd image store
278
279The containerd worker needs to be used
280
281```bash
282buildctl build ... --output type=image,name=docker.io/username/image
283ctr --namespace=buildkit images ls
284```
285
286To change the containerd namespace, you need to change `worker.containerd.namespace` in [`/etc/buildkit/buildkitd.toml`](./docs/buildkitd.toml.md).
287
288
289## Cache
290
291To show local build cache (`/var/lib/buildkit`):
292
293```bash
294buildctl du -v
295```
296
297To prune local build cache:
298```bash
299buildctl prune
300```
301
302### Garbage collection
303
304See [`./docs/buildkitd.toml.md`](./docs/buildkitd.toml.md).
305
306### Export cache
307
308BuildKit supports the following cache exporters:
309* `inline`: embed the cache into the image, and push them to the registry together
310* `registry`: push the image and the cache separately
311* `local`: export to a local directory
312
313In most case you want to use the `inline` cache exporter.
314However, note that the `inline` cache exporter only supports `min` cache mode.
315To enable `max` cache mode, push the image and the cache separately by using `registry` cache exporter.
316
317#### Inline (push image and cache together)
318
319```bash
320buildctl build ... \
321  --output type=image,name=docker.io/username/image,push=true \
322  --export-cache type=inline \
323  --import-cache type=registry,ref=docker.io/username/image
324```
325
326Note that the inline cache is not imported unless `--import-cache type=registry,ref=...` is provided.
327
328:information_source: Docker-integrated BuildKit (`DOCKER_BUILDKIT=1 docker build`) and `docker buildx`requires
329`--build-arg BUILDKIT_INLINE_CACHE=1` to be specified to enable the `inline` cache exporter.
330However, the standalone `buildctl` does NOT require `--opt build-arg:BUILDKIT_INLINE_CACHE=1` and the build-arg is simply ignored.
331
332#### Registry (push image and cache separately)
333
334```bash
335buildctl build ... \
336  --output type=image,name=localhost:5000/myrepo:image,push=true \
337  --export-cache type=registry,ref=localhost:5000/myrepo:buildcache \
338  --import-cache type=registry,ref=localhost:5000/myrepo:buildcache \
339```
340
341#### Local directory
342
343```bash
344buildctl build ... --export-cache type=local,dest=path/to/output-dir
345buildctl build ... --import-cache type=local,src=path/to/input-dir
346```
347
348The directory layout conforms to OCI Image Spec v1.0.
349
350#### `--export-cache` options
351-   `type`: `inline`, `registry`, or `local`
352-   `mode=min` (default): only export layers for the resulting image
353-   `mode=max`: export all the layers of all intermediate steps. Not supported for `inline` cache exporter.
354-   `ref=docker.io/user/image:tag`: reference for `registry` cache exporter
355-   `dest=path/to/output-dir`: directory for `local` cache exporter
356-   `oci-mediatypes=true|false`: whether to use OCI mediatypes in exported manifests for `local` and `registry` exporter. Since BuildKit `v0.8` defaults to true.
357
358#### `--import-cache` options
359-   `type`: `registry` or `local`. Use `registry` to import `inline` cache.
360-   `ref=docker.io/user/image:tag`: reference for `registry` cache importer
361-   `src=path/to/input-dir`: directory for `local` cache importer
362-   `digest=sha256:deadbeef`: digest of the manifest list to import for `local` cache importer.
363-   `tag=customtag`: custom tag of image for `local` cache importer.
364    Defaults to the digest of "latest" tag in `index.json` is for digest, not for tag
365
366### Consistent hashing
367
368If you have multiple BuildKit daemon instances but you don't want to use registry for sharing cache across the cluster,
369consider client-side load balancing using consistent hashing.
370
371See [`./examples/kubernetes/consistenthash`](./examples/kubernetes/consistenthash).
372
373## Expose BuildKit as a TCP service
374
375The `buildkitd` daemon can listen the gRPC API on a TCP socket.
376
377It is highly recommended to create TLS certificates for both the daemon and the client (mTLS).
378Enabling TCP without mTLS is dangerous because the executor containers (aka Dockerfile `RUN` containers) can call BuildKit API as well.
379
380```bash
381buildkitd \
382  --addr tcp://0.0.0.0:1234 \
383  --tlscacert /path/to/ca.pem \
384  --tlscert /path/to/cert.pem \
385  --tlskey /path/to/key.pem
386```
387
388```bash
389buildctl \
390  --addr tcp://example.com:1234 \
391  --tlscacert /path/to/ca.pem \
392  --tlscert /path/to/clientcert.pem \
393  --tlskey /path/to/clientkey.pem \
394  build ...
395```
396
397### Load balancing
398
399`buildctl build` can be called against randomly load balanced the `buildkitd` daemon.
400
401See also [Consistent hashing](#consistenthashing) for client-side load balancing.
402
403## Containerizing BuildKit
404
405BuildKit can also be used by running the `buildkitd` daemon inside a Docker container and accessing it remotely.
406
407We provide the container images as [`moby/buildkit`](https://hub.docker.com/r/moby/buildkit/tags/):
408
409-   `moby/buildkit:latest`: built from the latest regular [release](https://github.com/moby/buildkit/releases)
410-   `moby/buildkit:rootless`: same as `latest` but runs as an unprivileged user, see [`docs/rootless.md`](docs/rootless.md)
411-   `moby/buildkit:master`: built from the master branch
412-   `moby/buildkit:master-rootless`: same as master but runs as an unprivileged user, see [`docs/rootless.md`](docs/rootless.md)
413
414To run daemon in a container:
415
416```bash
417docker run -d --name buildkitd --privileged moby/buildkit:latest
418export BUILDKIT_HOST=docker-container://buildkitd
419buildctl build --help
420```
421
422### Podman
423To connect to a BuildKit daemon running in a Podman container, use `podman-container://` instead of `docker-container://` .
424
425```bash
426podman run -d --name buildkitd --privileged moby/buildkit:latest
427buildctl --addr=podman-container://buildkitd build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=oci | podman load foo
428```
429
430`sudo` is not required.
431
432### Kubernetes
433
434For Kubernetes deployments, see [`examples/kubernetes`](./examples/kubernetes).
435
436### Daemonless
437
438To run the client and an ephemeral daemon in a single container ("daemonless mode"):
439
440```bash
441docker run \
442    -it \
443    --rm \
444    --privileged \
445    -v /path/to/dir:/tmp/work \
446    --entrypoint buildctl-daemonless.sh \
447    moby/buildkit:master \
448        build \
449        --frontend dockerfile.v0 \
450        --local context=/tmp/work \
451        --local dockerfile=/tmp/work
452```
453
454or
455
456```bash
457docker run \
458    -it \
459    --rm \
460    --security-opt seccomp=unconfined \
461    --security-opt apparmor=unconfined \
462    -e BUILDKITD_FLAGS=--oci-worker-no-process-sandbox \
463    -v /path/to/dir:/tmp/work \
464    --entrypoint buildctl-daemonless.sh \
465    moby/buildkit:master-rootless \
466        build \
467        --frontend \
468        dockerfile.v0 \
469        --local context=/tmp/work \
470        --local dockerfile=/tmp/work
471```
472
473## Opentracing support
474
475BuildKit supports opentracing for buildkitd gRPC API and buildctl commands. To capture the trace to [Jaeger](https://github.com/jaegertracing/jaeger), set `JAEGER_TRACE` environment variable to the collection address.
476
477```bash
478docker run -d -p6831:6831/udp -p16686:16686 jaegertracing/all-in-one:latest
479export JAEGER_TRACE=0.0.0.0:6831
480# restart buildkitd and buildctl so they know JAEGER_TRACE
481# any buildctl command should be traced to http://127.0.0.1:16686/
482```
483
484## Running BuildKit without root privileges
485
486Please refer to [`docs/rootless.md`](docs/rootless.md).
487
488## Building multi-platform images
489
490See [`docker buildx` documentation](https://github.com/docker/buildx#building-multi-platform-images)
491
492## Contributing
493
494Want to contribute to BuildKit? Awesome! You can find information about contributing to this project in the [CONTRIBUTING.md](/.github/CONTRIBUTING.md)
495
496