Skip to content
James Ross
articlePublished Updated 8 min read

Capping Docker build memory with cgroup v2

By James Ross

A large Docker build on the container platform host consumed the host's memory and took every hosted site offline for about 21 minutes. The fix caps each BuildKit step at 24 GB with cgroup v2 memory.max, disables memory.high throttling, forbids swap, and kills the whole step group as one unit; a test build allocating 25 GB was OOM-killed in 21 seconds.

A build chamber with a capped memory gauge next to a row of lit site blocks, illustrating a Docker build memory limit using cgroup v2.

The host is an LXC container on Proxmox, so the platform and everything it serves share one memory budget; an unbounded build is a platform-wide incident, not a slow deploy. The same platform serves the sites moved off WordPress, which is why a build can be a production event.

Why can one Docker build take down a host?

Docker containers have no memory limit by default, so a build can use as much of the host’s memory as the kernel allows (Docker docs). A compiler, a bundler or an image conversion step that grows with the size of the input will happily take tens of gigabytes.

BuildKit makes this likely in two ways: it runs independent stages in parallel when the graph allows, and each instruction executes in its own container (BuildKit overview). One RUN step that allocates 12 GB, with three beside it, can put 40 GB of pressure on a host that never sees a --memory flag.

On this host the pressure hit the platform. The LXC container had a fixed memory budget, the build consumed it, and the containers serving the sites could not get pages. From outside, every hosted site went down at once for about 21 minutes; inside, one build was doing exactly what it was told.

Adding memory does not fix the shape of the problem; it changes how long the build takes to reach the new ceiling. The fix has to make one step fail before it can hurt anything else.

How does cgroup v2 memory accounting work?

cgroup v2 puts every process in a single hierarchy and exposes memory controls as files in the cgroup’s directory. Four of those files do the work here: memory.max, memory.high, memory.swap.max and memory.oom.group (kernel docs).

Control What it does Behaviour at the boundary
memory.max Hard limit on the cgroup’s memory Reclaim runs; if it fails, the OOM killer is invoked inside the cgroup
memory.high Throttling limit Tasks are slowed and forced to reclaim; no OOM kill
memory.swap.max Hard limit on swap Anonymous pages cannot be swapped past the limit
memory.oom.group Treats the cgroup as one workload All tasks in the cgroup are killed together, or none

The kernel documentation is explicit: memory.high throttles and memory.max is the last line of defence. systemd’s documentation agrees — MemoryHigh is the main mechanism, MemoryMax invokes the OOM killer when the limit cannot be held (systemd.resource-control).

For a build host, throttling is the wrong behaviour. A throttled compiler does not stop; it allocates more slowly, reclaims pages and keeps going. That suits a long-running service trading latency for memory, not a build step that must finish or die.

Why did memory.high make the build hang instead of fail?

The first version used memory.high as the control and left memory.max at 24 GB. In a 12 GB test, the build did not fail; it slowed, sat between the soft and hard limits, and never reached the hard limit that triggers the OOM killer.

That is memory.high working as designed: the kernel throttles the cgroup, forces direct reclaim, and the process keeps retrying. If the workload can make progress at a lower rate, it will, indefinitely. A build that takes 20 minutes longer is worse than one that fails in 21 seconds, because it holds the build lock while doing nothing useful.

The fix was to stop using memory.high for this job: set it to max, which disables throttling, and let memory.max do the only thing it does well. The step either fits in 24 GB or it is killed.

How does the 0.5-second cap service work?

A small systemd service polls the host’s system.slice cgroup directory every 0.5 seconds, finds scopes it has not configured yet, and writes the four control files. It is deliberately not a daemon that wraps the build; it does not need to know which project is building or how BuildKit was invoked.

The loop, reduced to its essentials:

# runs as root on the platform host; the loop sleeps 0.5s between passes
declare -A seen
while sleep 0.5; do
  for scope in /sys/fs/cgroup/system.slice/*.scope; do
    [ -e "$scope/memory.max" ] || continue        # not a memory cgroup
    [ -n "${seen[$scope]}" ] && continue          # already handled

    echo 24G > "$scope/memory.max"
    echo max > "$scope/memory.high"               # no throttling
    echo 0   > "$scope/memory.swap.max"           # no swap escape
    echo 1   > "$scope/memory.oom.group"          # kill the whole step
    seen[$scope]=1
  done
done

The service only touches scopes with a memory.max file, and it records each scope in memory so it never rewrites it. The cgroup filesystem does not accept marker files, so the tracker lives in the poller and resets on restart.

The 0.5-second interval leaves a window: a step that allocates gigabytes in its first half-second can exceed the cap before the service writes it. The backstops are the host container’s own memory limit, one build at a time, and the fact that most steps start a process before they allocate.

Why set memory.oom.group to 1?

memory.oom.group=1 makes the kernel treat the cgroup as one workload: when the OOM killer fires, every task in the cgroup is killed together, not just the largest process (kernel docs).

Without it, the OOM killer picks a victim — a compiler child, a linker, a shell — and the rest of the step continues half-broken. The build then fails later with a confusing error, or produces a partial artifact. With the flag set, the whole step dies at once, BuildKit reports a failed step, and the deploy stops.

This is the difference between “the build ran out of memory” and “the build is now in an unknown state”. For an unattended pipeline, the first is a signal and the second is a support ticket.

Why limit concurrent builds to one?

The cap is per step, not per build. Two builds at once can each have a step under 24 GB and still put 48 GB on a host that also runs the application containers. Concurrency one bounds the arithmetic: one build, one capped step, plus the platform’s working set.

The platform runs 19 or more applications and 28 projects, so the host is never idle. Concurrency one is a deliberate trade: deploys queue, but a build cannot starve the sites it is deploying. If the queue becomes the bottleneck, the answer is a separate build host, covered in the remote BuildKit post.

Did raising the host to 96 GB fix it?

No. Raising the container’s memory to 96 GB gave the platform headroom and made an accidental repeat less likely. It did not make an unbounded build safe: a build can always allocate more than the host has.

The cap changed the failure mode. A step over 24 GB is now killed inside its own cgroup while the platform keeps serving. The extra memory is margin, not a control.

The 25 GB test

The cap was verified with a test build that allocated 25 GB, comfortably over the 24 GB limit. The build was OOM-killed in 21 seconds. The platform stayed up.

That result is the whole point of the design. The build fails fast, the failure is attributable to one build, and the blast radius is the build. The 21 seconds also makes the failure cheap to retry after the build is fixed; it does not sit throttled for 20 minutes first.

Do not confuse the two numbers in this story: the outage lasted about 21 minutes, and the test build died in 21 seconds. They are not the same measurement.

Memory control options compared

Approach Where the limit applies At the limit Blast radius
No limit Host Kernel OOM killer picks any victim Anything on the host, including the platform
Host memory only Whole LXC container OOM killer inside the container; any app can be the victim Every hosted application
memory.high only Each build cgroup Throttling; the build slows and may hang Build queue, held lock, no clean failure
memory.max only Each build cgroup OOM kill inside the cgroup One process, possibly a child
memory.max plus swap 0 and oom.group=1 Each build cgroup Whole step killed, no swap escape One build step, cleanly

Which should you choose?

Use memory.max with memory.swap.max=0 and memory.oom.group=1 for any unattended build host. It produces a fast, attributable failure and protects everything outside the build.

Use memory.high for long-running services where slowing down is better than dying, not for batch work. If you want both, set memory.high below memory.max only when the workload can make progress under throttling and you have monitoring that acts on the throttle events.

If the host runs only builds and nothing else, a container-level limit may be enough. The moment the same host serves applications, the limit has to be per step, because the build is the thing that grows without warning.

What broke and what we changed

The first outage was a build, not an attack. The host ran out of memory while a large Docker build was running, and every hosted site became unreachable for about 21 minutes. Nothing was corrupted, but the platform had no mechanism to stop one workload from consuming the budget.

The first cap used the wrong control. Using memory.high as the control in a 12 GB test made the build hang between the soft and hard limits. The kernel was doing what memory.high is for. Removing the throttle and relying on memory.max turned the hang into a clean kill.

The cap had to target scopes, not containers. A limit on the build container does not necessarily cover every step BuildKit creates. Watching system.slice for new scopes catches each step as it appears, whatever the build tool thinks it is doing.

The poll interval is a compromise. 0.5 seconds is fast enough for steps that start by loading a toolchain and slow enough not to burn CPU on a host that is already busy. It is not a hard guarantee, which is why concurrency is one and the host has headroom.

More memory is not a fix. Raising the host to 96 GB reduced the chance of an accidental outage but did not bound the build. The cap is the control; the memory is the margin.

Frequently asked questions

Does Docker limit build memory by default?

No. Docker containers have no memory limit unless one is set, and a build step is a container. BuildKit will run steps in parallel when the graph allows it, so the peak can be several steps at once. On a shared host, that default is the incident waiting to happen (Docker docs).

What is the difference between memory.high and memory.max?

memory.high throttles the cgroup and forces reclaim when usage crosses it, but does not kill anything. memory.max is a hard limit: if reclaim cannot bring usage under it, the OOM killer runs inside the cgroup. Use memory.high to slow a service, and memory.max to stop a batch job.

Why did the build hang instead of getting killed?

Because memory.high was set below memory.max. The build crossed the soft limit, got throttled, and kept retrying allocations without ever reaching the hard limit. The kernel was throttling, not failing. Setting memory.high to max removed the throttle and let memory.max kill the step.

What does memory.oom.group=1 do?

It makes the kernel treat the cgroup as one workload. When the OOM killer fires, all tasks in the cgroup are killed together instead of one process being chosen. For a build step, that turns a partial, unknown state into a clean failed step that the pipeline can report and retry.

Why cap each BuildKit step instead of the whole build?

Because the build's peak is the sum of its parallel steps, and a per-build limit leaves each step free to grow. A per-step cap bounds the worst case, and memory.oom.group=1 makes the kill land on the step that caused it. The build fails at the step that is wrong, not at a random process.

Is 0.5-second polling fast enough?

For this host, yes: the steps that caused the incident took far longer than half a second to begin allocating. It is not a guarantee. If a step can allocate gigabytes in under 0.5 seconds, the cap lands late, which is why the host has headroom and builds run one at a time.

Does this work with cgroup v1?

No. The four control files here are cgroup v2 interfaces, and the host runs cgroup v2. On a v1 host the memory controller exposes a different file layout and the systemd unit settings differ. Check /sys/fs/cgroup/cgroup.controllers before assuming the same approach applies.