Sub topic 3
Setting a hard memory limit
This chapter assumes you can run docker run, docker ps, and docker inspect. It also assumes you know what RAM and swap are on a Linux host. By the end you will set a hard memory limit, read it back, and know what the kernel does when a container reaches it.
What a hard limit is
A hard memory limit is the maximum amount of RAM a container may use. You set it with --memory (short form -m). Without it, a container can use as much host memory as the kernel gives it. One leaking process can then starve every other container on the host.
The kernel enforces the limit through the container's memory cgroup. The limit covers more than application heap. It counts the container's process memory and the page cache the kernel holds on its behalf.
Setting the limit
Pass a number followed by a unit. Valid units are b, k, m, and g. The minimum value is 6 MiB.
docker run -d --name web --memory 256m --cpus 0.5 nginx:alpineThis starts a container that can use at most 256 MiB of memory and half of one CPU core. This chapter covers only the memory part. The --cpus flag comes later.
Reading the limit back
Docker stores the limit in bytes in the container's host configuration.
docker inspect --format 'mem={{.HostConfig.Memory}}' webFor 256m, the output is mem=268435456. A value of 0 means no limit is set.
What happens at the limit
When the container tries to use more memory than its limit, the kernel first tries to reclaim memory, for example by dropping cached file pages. If it cannot free enough, it kills a process in the container. This is an out-of-memory (OOM) kill.
The example below triggers one on purpose:
docker run --name hog --memory 50m --memory-swap 50m alpine sh -c 'tail /dev/zero'
docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' hogtail /dev/zero buffers the endless stream of zero bytes in memory until the 50 MiB limit is hit. Setting --memory-swap equal to --memory disables swap use, so the kernel kills the process. The inspect command typically reports true 137.
Exit code 137 is 128 plus 9, which means the process died from signal 9 (SIGKILL).
Common errors
- Trusting the exit code alone. Exit 137 also appears after
docker killordocker stoptimes out. Check.State.OOMKilledto tell the cases apart. - Limit too close to the working set. A process that normally uses 240 MiB will be killed under a 256 MiB limit once it spikes or the page cache grows. Measure real usage first.
- Only the main process dies. If the killed process is not PID 1, the container may keep running in a broken state. Look at the logs, not just the container status.
- Expecting a throttle. Memory is not compressible like CPU time. A container cannot be slowed to fit. It either stays under the limit or loses a process.
Exercise
A container started with --memory 128m exits with code 137. Which command tells you whether the kernel killed it for running out of memory, and what value confirms it?
Show answer
Run docker inspect --format '{{.State.OOMKilled}}' <container>. The value true confirms an OOM kill. Exit code 137 alone is not enough, because docker kill also produces it.
Exercise
What does docker inspect --format '{{.HostConfig.Memory}}' web print for a container started with --memory 256m, and what would 0 mean?
Show answer
It prints 268435456, which is 256 × 1024 × 1024 bytes. A value of 0 means no memory limit is set.