Skip to content

Docker resource limits — memory, CPU, PIDs, IO

Cap what containers can consume so one workload cannot starve another. The flags, the cgroups behind them, and the operational defaults we use on every host.

A container without resource limits is a process that can OOM the host, fork-bomb the PID table, eat every CPU cycle, or saturate the disk I/O queue. None of that is theoretical — we’ve seen all four in client incidents that started with “this should just be a small worker container.” This article covers the limits Docker exposes, how they map to Linux cgroups underneath, and the defaults we set on every container we run.

How to verify

Inspect the live resource configuration of running containers:

docker stats --no-stream
docker inspect <container> --format '{{.Name}} mem={{.HostConfig.Memory}} swap={{.HostConfig.MemorySwap}} cpus={{.HostConfig.NanoCpus}} pids={{.HostConfig.PidsLimit}}'

# Per-container cgroup state (cgroup v2)
cat /sys/fs/cgroup/system.slice/docker-$(docker inspect --format '{{.Id}}' <container>).scope/memory.max
cat /sys/fs/cgroup/system.slice/docker-$(docker inspect --format '{{.Id}}' <container>).scope/cpu.max
cat /sys/fs/cgroup/system.slice/docker-$(docker inspect --format '{{.Id}}' <container>).scope/pids.max

If Memory is 0, the container has no memory cap — it can grow until the host OOMs. If PidsLimit is 0 or -1, there’s no PID cap. docker stats shows live usage but won’t reveal limits.

What’s happening

Docker resource limits are a thin user-space facade over Linux cgroups (control groups). The runtime (runc via containerd) creates a cgroup for each container, writes limit values into the cgroup’s control files, and the kernel enforces them. Containers without limits get a cgroup with no caps — they can consume whatever the kernel will give them, which on a healthy host is “everything.”

The relevant subsystems:

  • memory — hard cap; processes are killed by the OOM killer when they exceed it.
  • cpu — quota and period (e.g. 100 ms quota per 100 ms period = 1 full CPU) or weight (proportional sharing).
  • pids — maximum number of processes/threads in the cgroup.
  • io (cgroup v2) — read/write IOPS and bandwidth per device.
  • blkio (cgroup v1) — same idea, older interface.

Modern hosts use cgroup v2 (default on RHEL 9+, Ubuntu 22+, Debian 12+). Some interfaces differ slightly from v1; docker info shows which is active.

The procedure

  1. Memory limits — the most important one. Pair --memory with --memory-reservation:
docker run -d --name app \
  --memory 512m \
  --memory-reservation 256m \
  --memory-swap 512m \
  myapp:latest
  • --memory 512m is the hard cap. Processes that allocate beyond this are killed by OOM.
  • --memory-reservation 256m is the soft floor. The scheduler considers it during memory pressure but does not enforce it.
  • --memory-swap 512m (set equal to --memory) disables swap usage. Without this, Docker defaults to twice the memory in swap, which silently hides memory pressure.

If you forget --memory-swap, a container will appear healthy while paging frantically — performance dies but it never OOMs.

  1. CPU limits. Two models, pick one:
# Hard cap: 1.5 CPUs maximum (period=100ms, quota=150ms)
docker run -d --cpus=1.5 myapp:latest

# Weight: 1024 is default share, 512 is half-weight
docker run -d --cpu-shares=512 myapp:latest

# Pinning to specific CPUs (NUMA, latency-sensitive workloads)
docker run -d --cpuset-cpus=0,1 myapp:latest

--cpus is the right choice for predictability. --cpu-shares is proportional — it only takes effect when CPUs are contended, and it’s relative to other shares on the host. Don’t mix the two on the same host without thinking through what happens under pressure.

  1. PID limits. Without this, a fork bomb in a container takes down the host:
docker run -d --pids-limit 200 myapp:latest

200 is generous for a single-process app, tight for a heavy multi-threaded one (Java with hundreds of threads will exceed this). Calibrate against docker exec <container> ps -eLf | wc -l.

  1. IO limits. Less commonly used but critical for noisy-neighbour mitigation:
# Read/write bandwidth limit per device
docker run -d \
  --device-read-bps  /dev/nvme0n1:50mb \
  --device-write-bps /dev/nvme0n1:50mb \
  --device-read-iops  /dev/nvme0n1:1000 \
  --device-write-iops /dev/nvme0n1:1000 \
  myapp:latest

This caps the container at 50 MB/s and 1000 IOPS to that device. Useful when a database container and a backup container share the same disk and the backup must not starve the database.

  1. ulimits at the container level. These apply to processes inside the container:
docker run -d \
  --ulimit nofile=65536:65536 \
  --ulimit nproc=4096:4096 \
  --ulimit memlock=-1:-1 \
  myapp:latest

nofile (file descriptors) defaults are often too low for connection-heavy services. memlock=-1 unlimits memory locking, required by services like Redis with persistence and some Java configurations.

  1. Set sensible defaults globally so every container starts limited. In /etc/docker/daemon.json:
{
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Soft": 65536,
      "Hard": 65536
    },
    "nproc": {
      "Name": "nproc",
      "Soft": 4096,
      "Hard": 8192
    }
  }
}

The daemon-level defaults give every container reasonable file descriptor and process headroom without per-container flags. Memory/CPU defaults still must be set per container — there’s no global memory cap.

  1. In Compose, the equivalent fields:
services:
  app:
    image: myapp:latest
    mem_limit: 512m
    mem_reservation: 256m
    memswap_limit: 512m
    cpus: 1.5
    pids_limit: 200
    ulimits:
      nofile:
        soft: 65536
        hard: 65536
  1. Watch for OOM kills. The kernel logs them; the cgroup tracks them:
journalctl -k | grep -iE "out of memory|oom-kill"
cat /sys/fs/cgroup/system.slice/docker-$(docker inspect --format '{{.Id}}' app).scope/memory.events
# look for oom_kill counter incrementing

A repeated OOM kill on the same container means the limit is too low OR the application has a leak. Triage which it is before just raising the limit.

Common pitfalls

  • Setting --memory without --memory-swap and watching containers thrash in swap rather than OOM — performance dies but no obvious signal fires.
  • Using --cpu-shares thinking it’s a hard cap. It’s a relative weight; under no contention, a --cpu-shares=64 container gets 100% of a CPU happily.
  • Forgetting --pids-limit on Java/Node containers and seeing one buggy thread pool exhaust the host’s pid table.
  • Setting per-container limits when running multiple containers per host without doing arithmetic — the sum of mem_limit across containers can exceed host memory.
  • Relying on cgroup v1 limit semantics on a v2 host. The interfaces moved; docker info confirms which version is active.

We treat resource limits as non-negotiable for every container we run. The defaults — memory, CPU, PIDs, ulimits — are baked into our base Compose templates and audited weekly as part of managed operations. An unlimited container is a host outage waiting for a deadline.