Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

A Docker container goes past its memory limit instead of stopping there

Your error

WARNING: No swap limit support2 more lines it can show

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.

Fix
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: 512m
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Docker Engine 29.10, Docker Compose 5.8, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
docker stats --no-stream my-app
docker info 2>&1 | grep -i swap

Then find the line you see in this list and open its fix:

  1. 1--memory is set, --memory-swap is not, so swap doubles the roomdocker stats shows the expected LIMIT, the host has swap, and the container slows down under load without being OOM killed
  2. 2The kernel has no swap accounting, so the swap limit is droppeddocker info ends with WARNING: No swap limit support, or docker run prints that the swap limitation was discarded
  3. 3Only a reservation was set, which is a soft floor and not a capThe 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 support
  • WARNING: 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.

Check
docker inspect --format '{{.HostConfig.Memory}} {{.HostConfig.MemorySwap}}' my-app

Fix

Give --memory-swap the same value as --memory, which leaves the container no swap. In Compose, set memswap_limit equal to mem_limit.

Fix
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: 512m

Verify

Under the same load the container now stops at the limit; docker inspect reports OOMKilled true if it crosses it.

Verify
docker inspect --format '{{.State.OOMKilled}}' my-app

Confirmed by docs.docker.com

Did this fix it?

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.

Check
grep GRUB_CMDLINE_LINUX= /etc/default/grub

Fix

Add the two parameters to GRUB_CMDLINE_LINUX in /etc/default/grub, update GRUB and reboot.

Fix
# /etc/default/grub
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"

sudo update-grub
sudo reboot

Verify

After the reboot docker info no longer prints the swap warning.

Verify
docker info 2>&1 | grep -i 'swap limit' || echo 'no swap warning'

Confirmed by docs.docker.com

Did this fix it?

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.

Check
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.

Fix
services:
  app:
    image: my-image
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M

Verify

After docker compose up -d, the LIMIT column of docker stats shows the 512M cap instead of the host total.

Verify
docker compose up -d
docker stats --no-stream

Confirmed by docs.docker.com

Did this fix it?

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

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.