Sub topic 6
Soft limits with --memory-reservation
This chapter assumes you know how to run containers with docker run, that you have read the chapter on hard limits with --memory, and that you know what RAM is and how the kernel reclaims it under pressure.
What a soft limit is
A hard limit (--memory) is a ceiling. If the container goes over it, the kernel reclaims memory from that container or kills a process in it.
A soft limit (--memory-reservation) is a target. Docker sets it through the cgroup's soft limit. The kernel ignores it while the host has plenty of free memory, so the container can use more than the reservation. When the host comes under memory pressure, the kernel reclaims memory from containers that are above their reservation and pushes them back toward it.
docker run -d --name soft --memory 512m --memory-reservation 256m nginx:alpineThis container may grow to 512 MiB. When the host is short on memory, the kernel tries to shrink it toward 256 MiB.
How the two limits interact
The reservation must be lower than the hard limit. Docker rejects a reservation that is larger than --memory. You can also set a reservation without a hard limit. The container then has no ceiling, but it is still reclaimed toward its reservation under pressure.
The table summarizes the difference.
| Property | `--memory` | `--memory-reservation` |
|---|---|---|
| Type | Hard limit | Soft limit |
| Enforced | Always | Only under host memory pressure |
| Exceeding it | Reclaim, then OOM kill | Reclaim toward the value |
| Must be lower than the other | No | Yes, lower than `--memory` |
Checking the setting
Docker records the reservation in bytes. Read it with docker inspect.
docker inspect --format 'hard={{.HostConfig.Memory}} soft={{.HostConfig.MemoryReservation}}' softFor the container above, the output is hard=536870912 soft=268435456.
When to use it
A reservation fits hosts that run several containers whose peak loads do not coincide. Give each one a hard limit that caps the worst case and a reservation near its normal use. Idle containers then yield memory first when the host gets tight.
Keep these points in mind:
- A reservation does nothing on a host that never runs short of memory, so you cannot see its effect on an idle machine.
- It does not prevent OOM kills. Only the hard limit sets the point where the container is killed.
- Reclaiming memory can slow an application, because the kernel may drop cached pages or push memory to swap.
- Orchestrators use reservation values for scheduling. Plain
docker rundoes not schedule anything.
Exercise
Exercise
A container runs with --memory 1g --memory-reservation 400m on a host with lots of free memory. The container's process uses 800 MiB. Is it reclaimed to 400 MiB, killed, or left alone? What changes if the host becomes short of memory?
Show answer
It is left alone. Usage is below the 1 GiB hard limit, and the reservation only applies under host memory pressure. If the host becomes short of memory, the kernel reclaims memory from the container toward 400 MiB. It is killed only if usage exceeds 1 GiB and the kernel cannot reclaim enough.
Exercise
Why does docker run --memory 256m --memory-reservation 512m nginx:alpine fail?
Show answer
The soft limit must be lower than the hard limit. A reservation of 512 MiB is higher than the 256 MiB ceiling, so Docker rejects the command.