Skip to content

Resource Limits

Without limits, a misbehaving container can starve its host. Set sensible CPU and memory caps for every long-running service.

services:
my-app:
image: ghcr.io/trueforge-org/my-app:latest
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M

For Compose v2 outside of Swarm, the equivalent shorthand is:

services:
my-app:
image: ghcr.io/trueforge-org/my-app:latest
cpus: 1.0
mem_limit: 512m
mem_reservation: 128m
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 1000m
memory: 512Mi
  • Start by observing the application under realistic load (docker stats, kubectl top).
  • Set requests to the steady-state usage; this drives scheduling.
  • Set limits to a comfortable ceiling — typically 1.5×–2× the steady state.
  • Avoid setting CPU limits aggressively in Kubernetes: throttling hurts latency-sensitive workloads more than it helps.

If a container exceeds its memory limit it is killed (OOMKilled). Combine limits with healthchecks and a restart policy so the workload recovers automatically.