build: Add per-step CPU and memory resource limits

Port of moby/buildkit#6569 to buildx. Adds --memory, --memory-swap,
--cpu-shares, --cpu-period, --cpu-quota, --cpuset-cpus, and --cpuset-mems
flags to build, plus the equivalent bake target attributes and compose
x-bake fields.

Signed-off-by: Jiří Moravčík <jiri.moravcik@gmail.com>
This commit is contained in:
Jiří Moravčík
2026-06-08 15:39:14 +02:00
parent 4fbc1e5f2c
commit 80b2293881
14 changed files with 761 additions and 48 deletions
+24
View File
@@ -239,6 +239,7 @@ The following table shows the complete list of attributes that you can assign to
| [`policy`](#targetpolicy) | List | Policies to validate build sources and metadata |
| [`platforms`](#targetplatforms) | List | Target platforms |
| [`pull`](#targetpull) | Boolean | Always pull images |
| [`resources`](#targetresources) | Map | Resource limits for build containers |
| [`secret`](#targetsecret) | List | Secrets to expose to the build |
| [`shm-size`](#targetshm-size) | List | Size of `/dev/shm` |
| [`ssh`](#targetssh) | List | SSH agent sockets or keys to expose to the build |
@@ -939,6 +940,29 @@ target "default" {
}
```
### `target.resources`
Sets cgroup resource limits for the containers that run `RUN` instructions
during the build. The supported keys are `memory`, `memory-swap`, `cpu-shares`,
`cpu-period`, `cpu-quota`, `cpuset-cpus`, and `cpuset-mems`. These map to the
equivalent `docker build` flags and to the
[`--resource`](https://docs.docker.com/reference/cli/docker/buildx/build/#resource)
flag for `docker buildx build`.
```hcl
target "default" {
resources = {
memory = "2g"
memory-swap = "4g"
cpu-quota = 50000
}
}
```
> [!NOTE]
> These limits require a BuildKit daemon that supports per-step resource limits
> and only take effect on Linux. They don't affect the build cache key.
### `target.secret`
Defines secrets to expose to the build target.