Updated November 2026: Updated for Docker Engine 29 and cgroups v2: new multi-arch test commands, --memory-reservation, OOM kills with exit code 137, docker update and Docker Compose limits.

By default, Docker does not limit the memory of a container. A single container can take all the memory of its host, and one leaking process slows down everything else on the machine. A memory limit turns that into a predictable failure: the container gets killed, the rest of the host keeps running.

In this post I show how --memory, --memory-swap and --memory-reservation work, what they map to in cgroups v2, and how to spot a container that got OOM killed.

What is the host on Docker Desktop?

If you use Docker Desktop on macOS or Windows, containers run inside a Linux VM. That VM is the host for all limits in this post. Its total memory and swap are set in Docker Desktop under Settings > Resources > Advanced (with the WSL 2 backend on Windows, in %UserProfile%\.wslconfig instead). A container without a limit can use everything that VM has, but nothing beyond it.

You can check what your containers see with docker info:

BASH
docker info --format 'cgroup v{{.CgroupVersion}}, {{.MemTotal}} bytes, swap limit support: {{.SwapLimit}}'

The memory flags of docker run

Flag What it does cgroups v2 file
-m, --memory Hard limit for RAM, minimum 6m memory.max
--memory-swap Hard limit for RAM plus swap memory.swap.max (the swap part only)
--memory-reservation Soft limit, must be lower than --memory memory.low

The tricky one is --memory-swap. It is not the amount of swap, it is the total of RAM and swap:

  • --memory-swap not set: the container may use as much swap as --memory, so -m 128m allows 128 MiB of RAM plus 128 MiB of swap.
  • --memory-swap equal to --memory: no swap at all.
  • --memory-swap larger than --memory: the difference is the swap budget, e.g. -m 128m --memory-swap 192m gives 64 MiB of swap.
  • --memory-swap -1: unlimited swap, bounded only by the swap of the host.

--memory-reservation is not a limit the container hits. Docker writes it to memory.low, which protects that much memory from reclaim when the host runs low. Docker does not set memory.high, so there is no throttling before the hard limit.

You can see the mapping from inside any container:

BASH
docker run --rm -m 128m --memory-swap 192m --memory-reservation 64m alpine:3.22 \
  cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/memory.swap.max /sys/fs/cgroup/memory.low

134217728
67108864
67108864

--memory-swappiness and --oom-kill-disable only work with cgroups v1. On a cgroups v2 host (Docker Desktop and every current Linux distribution), Docker prints a warning and ignores them.

What happens when a container hits its limit

Docker memory limits: RAM, swap and the OOM kill

While the container uses less than --memory, everything stays in RAM. Once it reaches memory.max, the kernel reclaims memory from that container and moves pages to swap, as long as the swap budget allows it. This keeps the process alive but makes it a lot slower. When RAM and swap are both used up, the kernel OOM killer sends SIGKILL to a process in the container. If that process is the main process, the container stops with exit code 137 (128 + signal 9).

Testing Docker memory limits

To test the limits, I use a Python one-liner that allocates and fills the given number of MiB. The python:3.14-alpine image is multi-arch, so this works on Intel and Apple silicon alike:

BASH
HOG='import sys; b = b"x" * int(sys.argv[1]) * 1024 * 1024; print("allocated", sys.argv[1], "MiB")'

Stay below the memory limit

BASH
docker run --rm -m 128m python:3.14-alpine python3 -c "$HOG" 96

allocated 96 MiB

Use the implicit swap

Without --memory-swap, a container limited to 128 MiB can use another 128 MiB of swap. Allocating 200 MiB still works:

BASH
docker run --rm -m 128m python:3.14-alpine python3 -c "$HOG" 200

allocated 200 MiB

Exceed RAM plus swap

300 MiB is more than 128 MiB of RAM plus 128 MiB of swap. This time I leave out --rm, so I can inspect the container afterwards:

BASH
docker run --name hog -m 128m python:3.14-alpine python3 -c "$HOG" 300
echo $?

137

Nothing gets printed. The process is killed before it finishes the allocation. docker inspect tells you why:

BASH
docker inspect hog --format 'OOMKilled={{.State.OOMKilled}} ExitCode={{.State.ExitCode}}'

OOMKilled=true ExitCode=137

docker rm hog

Exit code 137 alone only tells you the process got SIGKILL. OOMKilled=true confirms that the memory limit was the reason. If the OOM killer picks a child process instead of the main process, the container may keep running, so also watch for oom events with docker events --filter event=oom.

Disable swap

Set --memory-swap to the same value as --memory to get a hard RAM limit without swap:

BASH
docker run --rm -m 128m --memory-swap 128m python:3.14-alpine python3 -c "$HOG" 150
echo $?

137

This is what I use for most services: a container that swaps is usually just a slow container, and I would rather see the OOM kill.

Set the swap budget explicitly

With -m 128m --memory-swap 192m, the container gets 64 MiB of swap. 160 MiB fits, 200 MiB does not:

BASH
docker run --rm -m 128m --memory-swap 192m python:3.14-alpine python3 -c "$HOG" 160

allocated 160 MiB

docker run --rm -m 128m --memory-swap 192m python:3.14-alpine python3 -c "$HOG" 200
echo $?

137

Allow unlimited swap

--memory-swap -1 removes the swap limit for the container. It can still only use what the host has. On Docker Desktop that is the swap size configured for the VM (1 GB by default):

BASH
docker run --rm -m 128m --memory-swap -1 python:3.14-alpine python3 -c "$HOG" 512

allocated 512 MiB

Watch and change limits of running containers

docker stats shows the current usage against the limit:

BASH
docker run -d --name web -m 256m nginx:alpine
docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}' web

NAME      MEM USAGE / LIMIT   MEM %
web       8.934MiB / 256MiB   3.49%

You don’t have to recreate a container to change its limits. docker update writes the new values to the cgroup right away. Always pass --memory-swap together with --memory: the container above has an implicit total of 512 MiB, and docker update -m 512m web alone keeps that total, which silently leaves it with no swap at all.

BASH
docker update -m 512m --memory-swap 1g web
docker rm -f web

Memory limits in Docker Compose

Docker Compose applies deploy.resources with a plain docker compose up, no Swarm needed. Swap is set with memswap_limit on the service, and like on the CLI it is the total of RAM and swap:

YAML
services:
  app:
    image: python:3.14-alpine
    deploy:
      resources:
        limits:
          memory: 256M
        reservations:
          memory: 128M
    memswap_limit: 512M

This results in memory.max of 256 MiB, memory.low of 128 MiB and 256 MiB of swap. CPU limits work the same way; I cover them in Docker container CPU limits explained .

Conclusion

Set --memory on every container you run for longer than a few minutes, decide on purpose how much swap it gets, and check OOMKilled in docker inspect whenever a container exits with 137. That way a memory leak shows up as a clear OOM kill of one container instead of a slow, unresponsive host.