A Docker container goes past its memory limit instead of stopping there
Fix it fast
Most common cause, 1 of 3--memory is set, --memory-swap is not, so swap doubles the room
Give --memory-swap the same value as --memory, which leaves the container no swap.
docker run -d --name my-app --memory 512m --memory-swap 512m my-image
# compose.yaml
services:
app:
image: my-image
mem_limit: 512m
memswap_limit: 512mShort answer
A Docker --memory limit that seems ignored can be swap at work: with --memory set and --memory-swap unset, the container may use the same amount again in swap, so it slows down instead of being killed. Set --memory-swap equal to --memory, enable swap accounting when docker info warns No swap limit support, and use limits, not reservations. Docker Engine 29.
Which cause do you have?
Read the limit Docker really applied and look for the swap warning. docker stats shows the limit next to current use, and docker info ends with any kernel capability warning.
docker stats --no-stream my-app
docker info 2>&1 | grep -i swapThen find the line you see in this list and open its fix:
- 1--memory is set, --memory-swap is not, so swap doubles the room
docker stats shows the expected LIMIT, the host has swap, and the container slows down under load without being OOM killed - 2The kernel has no swap accounting, so the swap limit is dropped
docker info ends with WARNING: No swap limit support, or docker run prints that the swap limitation was discarded - 3Only a reservation was set, which is a soft floor and not a cap
The run command uses --memory-reservation, or the Compose file sets deploy.resources.reservations.memory or mem_reservation, with no matching limit
What does the error look like?
WARNING: No swap limit supportWARNING: Your kernel does not support swap limit capabilities. Limitation discarded.The app keeps growing past its --memory value into swap, gets slow and is not OOM killed
Cause 1 of 3
--memory is set, --memory-swap is not, so swap doubles the room
You seedocker stats shows the expected LIMIT, the host has swap, and the container slows down under load without being OOM killed
Check
A memory limit that does not stop the container can be the swap allowance Docker adds on top of it. Docker's resource constraints page says that with --memory set and --memory-swap unset, the container may use as much swap as the memory setting, so 300m of memory becomes 600m of memory plus swap. It also warns that free inside the container shows the host's swap, not the container's.
docker inspect --format '{{.HostConfig.Memory}} {{.HostConfig.MemorySwap}}' my-appFix
Give --memory-swap the same value as --memory, which leaves the container no swap. In Compose, set memswap_limit equal to mem_limit.
docker run -d --name my-app --memory 512m --memory-swap 512m my-image
# compose.yaml
services:
app:
image: my-image
mem_limit: 512m
memswap_limit: 512mVerify
Under the same load the container now stops at the limit; docker inspect reports OOMKilled true if it crosses it.
docker inspect --format '{{.State.OOMKilled}}' my-appConfirmed by docs.docker.com
Cause 2 of 3
The kernel has no swap accounting, so the swap limit is dropped
You seedocker info ends with WARNING: No swap limit support, or docker run prints that the swap limitation was discarded
Check
The swap part of a Docker memory limit is ignored when the kernel cannot account for swap, and Docker says so in a warning. Docker's daemon troubleshooting page lists the message for Ubuntu and Debian hosts and gives kernel boot parameters to turn the capability on, at a cost of about 1% of memory and a 10% overall slowdown.
grep GRUB_CMDLINE_LINUX= /etc/default/grubFix
Add the two parameters to GRUB_CMDLINE_LINUX in /etc/default/grub, update GRUB and reboot.
# /etc/default/grub
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
sudo update-grub
sudo rebootVerify
After the reboot docker info no longer prints the swap warning.
docker info 2>&1 | grep -i 'swap limit' || echo 'no swap warning'Confirmed by docs.docker.com
Cause 3 of 3
Only a reservation was set, which is a soft floor and not a cap
You seeThe run command uses --memory-reservation, or the Compose file sets deploy.resources.reservations.memory or mem_reservation, with no matching limit
Check
A container that ignores its memory value may have been given a reservation instead of a limit. The Compose deploy reference defines limits as what the platform must prevent the container from going over, and reservations as an amount it must be able to get; Docker's resource page calls --memory-reservation a soft limit that does not keep the container below it.
docker compose config | grep -n -A3 -E 'limits|reservations|mem_'Fix
Put the cap under limits (or use mem_limit), and keep a reservation only as an extra, lower value.
services:
app:
image: my-image
deploy:
resources:
limits:
memory: 512M
reservations:
memory: 256MVerify
After docker compose up -d, the LIMIT column of docker stats shows the 512M cap instead of the host total.
docker compose up -d
docker stats --no-streamConfirmed by docs.docker.com
How do I stop it coming back?
- Set --memory and --memory-swap (or mem_limit and memswap_limit) as a pair on every production container.
- Run docker info on each new host and enable swap accounting before you rely on swap limits.
- Read docker compose config in CI so a limit that landed under reservations is caught before deploy.
Sources
- Docker: resource constraints (memory and swap)
- Docker: daemon troubleshooting, swap limit capabilities
- Compose: deploy specification (limits and reservations)
- Compose: container spec (mem_limit, memswap_limit)
- Docker: docker container stats
- Docker: docker compose config
- Moby API types: HostConfig resources (Memory, MemorySwap)
- Moby API types: container State (OOMKilled)
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Dispatch Pro.