Skip to content
VDS · Docker and containers

Memory and CPU limits for containers

How to set a memory and CPU ceiling for a container, why without it one service brings down all the others, and how to tell that a process was killed by the kernel.

Updated August 23, 2026

By default, a container can take all the memory and all the cores on the machine. On VDS-1 with two gigabytes, this ends predictably: one service balloons, the kernel starts killing processes, and what goes down isn't it but the database next to it.

How to set a ceiling

services:
  app:
    image: ghcr.io/example/app:1.5.0
    deploy:
      resources:
        limits:
          memory: 768M
          cpus: "1.0"
        reservations:
          memory: 256M

Compose applies deploy.resources.limits even with a plain docker compose up -d. The equivalent for docker run:

docker run -d --memory 768m --cpus 1.0 --memory-swap 768m <image>

Setting --memory-swap equal to --memory forbids swapping: an honest crash is better than a machine that barely moves for hours.

How much to allocate

Start from your tier and leave the system some headroom.

TierMemoryReasonable total container ceiling
VDS-12 GBup to 1.5 GB
VDS-24 GBup to 3 GB
VDS-38 GBup to 6.5 GB
VDS-416 GBup to 14 GB
VDS-524 GBup to 21 GB
VDS-632 GBup to 29 GB

The remainder is needed by the system, the file cache and Docker itself. A sum of limits greater than the amount of memory isn't a configuration mistake — it's a disaster waiting to happen.

For the CPU: cpus: "1.0" is one full core. VDS-1 has a single core, so there's little point in limiting the CPU there, but on VDS-4 and above it's worth keeping the server setup from taking all six.

What is actually consumed

docker stats --no-stream
docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.CPUPerc}}"
free -h

Watch the service for a day under load and set the limit at roughly one and a half times the peak. A limit that's too tight is worse than none: the service will restart right when you need it most.

Full-screen htop and btop only work in the “Screen” console (VNC). The text console doesn't pass the window size, it's always 80×24 there, and the interface falls apart. For quick measurements in the text console, use docker stats --no-stream and free -h.

How to tell that it was killed by memory

docker inspect -f '{{.State.ExitCode}} {{.State.OOMKilled}}' <container>
sudo dmesg -T | grep -i -E "out of memory|killed process"

Exit code 137 together with OOMKilled: true is an unambiguous verdict. From there you have two options: raise the limit if it was set too low, or figure out why the application keeps growing — a leak isn't cured by adding memory, only postponed.

Java and other special cases

The Java virtual machine still sometimes sees the host's memory rather than the container limit, and sizes the heap with headroom that isn't there. Set it explicitly:

environment:
  JAVA_TOOL_OPTIONS: "-Xmx512m -Xms256m"

The same applies to PostgreSQL (shared_buffers) and any service with its own pool.

Common mistakes

  • No limits set at all. The most common cause of “the machine froze, nothing responds”.
  • The limit is there, but there's no health check. The container was killed and never restarted. Add restart: unless-stopped and a healthcheck — see updating containers.
  • You forgot about the disk. You limited memory, but writes run into a ceiling: see disk speed.