Docker resource limits for a home lab: memory, CPU, and PIDs in Compose
What stops one container from eating the whole host? By default, nothing. Docker's own documentation opens with the plain fact: a container starts with no resource constraints and can use as much of a given resource as the host's kernel scheduler allows. On a machine running one service, that default is harmless. On a home lab host running a dozen, it means the least important container on the box, a leaking sidecar, a runaway transcode, gets to decide whether everything else, including SSH, stays alive. Limits are how that decision gets taken back.
Kill versus throttle: the two limits behave differently
Memory and CPU limits are not the same kind of fence, and setting
them as if they were is where most copied configurations go wrong.
A memory limit is a hard ceiling with a fatal consequence.
Docker enforces hard limits that let a container use no more than a
fixed amount of memory; past it, the kernel's out-of-memory mechanism
kills processes to free memory. Docker softens the blast radius by
adjusting the daemon's own OOM priority so the daemon is less likely
to be killed, while a container's priority is not adjusted, the
container dies, the host survives. Under Compose, a container killed
this way exits with code 137, and a restart policy such as
unless-stopped brings it back, which converts a leak into
a visible restart loop instead of a silent host failure.
A CPU limit never kills anything. Exceed it and Docker throttles: the container keeps running, more slowly. That asymmetry drives the whole strategy. Memory limits protect the host from the container. CPU limits protect the other containers from queueing behind it, and because the penalty is slowness rather than death, CPU caps can be set generously on bursty services like media servers without risking a kill.
The controls, checked against the docs
| Control | Compose key | Runtime flag | Behaviour |
|---|---|---|---|
| Memory limit | deploy.resources.limits.memory | --memory | Hard cap; minimum value 6m; container is OOM-killed past it |
| Memory reservation | deploy.resources.reservations.memory | --memory-reservation | Soft floor, activated under contention or low memory; must be lower than the limit |
| Swap allowance | Compose memory-swap setting | --memory-swap | Total memory plus swap; set equal to the memory limit to disable swap |
| CPU limit | deploy.resources.limits.cpus | --cpus | Ceiling in cores, 1.5 means at most one and a half cores; throttles, never kills |
| PID limit | deploy.resources.limits.pids | --pids-limit | Caps process count, containing fork-heavy runaway behaviour |
The Compose specification defines the two halves precisely:
limits mean the platform must prevent the container allocating
more, and reservations mean it must guarantee the container
can allocate at least that much. Two cautions ride on that wording.
First, deploy is an optional part of the
Compose Specification, so the block's presence in a file is not proof
of enforcement, verification is a step below, not an assumption.
Second, the older relative control, --cpu-shares, is a
soft weight with a default of 1024 that only matters when CPU cycles
are contended; it guarantees nothing. Reach for --cpus
when a ceiling is the intent.
Measure, cap, verify: the sizing procedure
- Measure before limiting anything. Run
docker statson the host during normal use and again during the service's busy period, a library scan, a backup window. NoteMEM USAGE / LIMITandCPU %per container. CPU percentage can exceed 100% on a multi-core host; that is one busy core, not an error. - Set the memory limit above the observed peak, with headroom. Peak observed usage plus roughly half again is a defensible starting point for a household service: tight enough to contain a leak, loose enough that a busy evening does not trigger a kill. A limit copied from a tutorial's example stack is a guess about somebody else's workload.
- Set the reservation below the limit. The
reservation only takes precedence when it is lower than
--memory, and it does not guarantee the container stays under it, it is the amount Docker tries to protect for that container when the host is under memory pressure. Half the limit is a reasonable default relationship. - Cap CPU where contention is real. On a small host, give interactive services their headroom and cap the batch workers, transcoders, indexers, importers, at a whole-core figure the host can spare. Leave latency-sensitive small services uncapped if the host is never CPU-bound; a cap that never binds is decoration.
- Apply through Compose and redeploy with
docker compose up -d, so the limits live in the file the lab is actually run from, not in a one-off run command. - Verify in the same tool used to measure. Run
docker statsagain: theLIMITcolumn must now show the configured cap for each service, not the host's total memory. Then watch the service through one busy period.
What success looks like: every service shows its
intended limit in docker stats, nothing restarts with
exit code 137 during a normal busy period, and the host stays
responsive. SSH included, while the heaviest container is working.
The swap loophole, and two settings to leave alone
One default quietly undoes a memory cap. --memory-swap
is the total of memory plus swap a container may use. Set
--memory="300m" and --memory-swap="1g" and
the container gets 300 MB of memory and 700 MB of swap. Leave
--memory-swap unset entirely, and the container can use
as much swap as its memory setting, the same 300 MB cap becomes
600 MB in total. On a host where swap sits on a small SSD, that is
both a bigger cap than intended and a slower failure. Setting
--memory-swap equal to --memory closes it:
the container gets no swap access. Whether to close it is a judgement
call, swap can be a useful buffer against brief spikes, but it
should be a decision, not a default nobody noticed.
Two settings from the same documentation page stay untouched in a
home lab. Docker explicitly warns against circumventing its safeguards
with an extreme negative --oom-score-adj, and against
--oom-kill-disable except on containers that also carry
a memory limit, disabling the killer on an uncapped container hands
the OOM decision back to the kernel, with the whole host as the
candidate list. The --memory-swappiness knob (0 to 100,
inherited from the host by default) can stay inherited too, until
there is a measured reason to tune it.
Who gets limits first
Order matters more than completeness. Limit in this order: anything that processes untrusted or unbounded input (importers, converters, downloaders), anything with a history of growing (media servers with expanding libraries), then everything else as the stats data justifies it. The database and the password vault get conservative limits set from long observation, because an OOM kill there is a household event. A host where only the risky third of the stack is capped is already most of the way to the actual goal, which was never tidy numbers. It was making sure the next runaway process files its failure report as one restarted container, not as a dark house.
, Dex Carter, Tech Writer