Skip to content

Docker audit fix · Runtime & Resources

How to Configure Memory Limits for Docker Containers

A Docker container started without --memory has no cgroup memory ceiling and can consume every byte of host RAM, after which the kernel OOM killer picks victims anywhere on the box. Give each container a hard limit sized from its real peak usage.

Hiren KalariyaHiren KalariyaLast reviewed memory-limits
CtrlOpsMemory LimitsMEDIUM

What did your audit say?

WARN · when the audit printed

Some have none

At least one running container can grow into all host RAM; the message gives the count and names up to five of them.

Severity
Medium
Needs root or sudo
Yes
Results it can return
WARN · SKIP · PASS
Check in this audit
3 of 6

Every threshold on this page is transcribed from the docker-security-runtime-resources audit script that CtrlOps runs, and a build check fails if the two ever disagree. See all 6 Runtime & Resources checks, or how the audit runs.

The check runs docker inspect over every running container id with the template {{if eq .HostConfig.Memory 0}}{{.Name}} {{end}}, so it reads exactly one field per container: .HostConfig.Memory, the hard limit in bytes. A value of 0 is Docker's way of recording no limit and puts that container's name on the list, while any non-zero value passes however large or small it is. An empty list is the PASS; a non-empty one is the WARN, whose message gives the count against the number of running containers and names up to five of them.

Needs a reachable daemon and at least one running container; otherwise SKIP. It tests one field per running container: .HostConfig.Memory == 0, which is Docker's representation of "no limit". Any non-zero value passes, however large or small - the check does not judge whether the limit is sensible, only that one exists. It does not look at MemoryReservation (a soft limit, which does not satisfy this check), at swap limits, or at whether the container is anywhere near its ceiling.

What each result means
ResultWhenWhat it means
SKIPNo running containersNothing was verified. No container's memory setting was read, so this says nothing about whether limits exist.
PASSEvery container has --memoryEvery running container carries a non-zero hard memory limit, though the check never judges whether the value is sensible.
WARNSome have noneAt least one running container can grow into all host RAM; the message gives the count and names up to five of them.

Without a cgroup memory limit, one leaking or attacked container can consume all host RAM; the kernel OOM killer then picks victims across the whole host (databases, sshd). CIS Docker 5.10 "Ensure that the memory usage for containers is limited"; OWASP Docker Cheat Sheet Rule #7 "Limit resources (memory, CPU, file descriptors, processes, restarts)"; NIST SP 800-190 4.4.

  • Only Memory (hard limit) is checked; MemoryReservation (soft), MemorySwap and OomKillDisable are not.
  • In Compose, either deploy.resources.limits.memory or the service-level mem_limit sets the hard limit; deploy.resources.reservations.memory and mem_reservation set only the soft limit and leave this check failing.
  • On cgroup v1 hosts without swap accounting enabled (cgroup_enable=memory swapaccount=1 on older kernels), swap limits are ignored (Docker warns).
  • Setting a limit too low causes OOM kills of the container itself (exit code 137); size it from docker stats peak usage plus headroom.
  1. Measure real peak usage

    Watch the containers over a normal working period, including any batch jobs, and note each one's peak. Add about 20 percent headroom to that figure.

    sh
    docker stats --format 'table {{.Name}}\t{{.MemUsage}}'

  2. Set the limit

    Setting --memory-swap to the same value stops the container swapping, so a leak fails fast instead of dragging the host into swap.

    sh
    docker run --memory 512m --memory-swap 512m ...

    In Compose:

    yaml
    services:  app:    deploy:      resources:        limits:          memory: 512M

  3. Apply it

    Recreate the container so it starts with the new limit. For a running container you cannot restart yet, docker update changes the limit in place; put the same value in the Compose file too, or the next recreate drops it.

    sh
    docker compose up -d --force-recreate# or, in place:docker update --memory 512m --memory-swap 512m <name>

  4. Confirm the limit

    sh
    docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}'

    The value after the slash should be your limit, not the host's total RAM.

sh
docker inspect -f '{{.Name}} mem={{.HostConfig.Memory}} swap={{.HostConfig.MemorySwap}}' $(docker ps -q)# expected: a non-zero byte value for every containerdocker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}'# expected: LIMIT is the value you set, not the host's total RAM# What the container itself sees (cgroup v2):docker exec <name> cat /sys/fs/cgroup/memory.max# expected: the limit in bytes, not "max"

Debugging

  • This one message covers two different cases: the audit could not reach the Docker daemon, or it reached it and nothing was running. Run docker ps as the audit user, then sudo docker ps. If both fail, the daemon is unreachable: check systemctl status docker, then run the audit as root or give the account sudo. If they work but list nothing, there is genuinely nothing to evaluate, and a stopped container is never checked. The Docker Inventory check reports the two cases separately, so its result tells you which one you hit.
  • Truncated with head -5; the N of M count is complete. Full list: docker inspect -f '{{if eq .HostConfig.Memory 0}}{{.Name}}{{end}}' $(docker ps -q).
  • Current docker compose honours both keys, so the usual causes are elsewhere: the container was not recreated after the edit (docker compose up -d --force-recreate), the key sits under reservations instead of limits, or it is indented under the wrong service. docker compose config prints the file as Compose resolves it, which shows all three. The old --compatibility flag was only needed by the retired Python docker-compose v1.
  • That is the limit doing its job, but the value may be too low. Check with docker inspect -f '{{.State.OOMKilled}}' <name> and size it from real usage (docker stats), leaving headroom. For a JVM, also set -XX:MaxRAMPercentage so the runtime respects the cgroup.
  • A limit exists but may be larger than the host can honour across all containers. Add up the limits and compare against free -h; also check that swap is not masking the problem (MemorySwap).

Sources

Next

Re-run the Runtime & Resources audit after applying the fix and confirm this check moves to PASS. CtrlOps runs all 6 checks over your existing SSH connection and scores the result, so the change is visible without reading another config file.

Download for MacDownload for WindowsAll 6 Runtime & Resources fixes

14-day free trial, no credit card required.

Explore article with AI tools

Written by

Hiren Kalariya

Co-Founder & CEO, TST Technology

Hiren Kalariya is the Co-Founder and CEO of TST Technology and the product lead behind CtrlOps, a local-first desktop app that makes Linux server management fast and easy for developers and DevOps teams. Over the past five years he has shipped 60+ production systems, and he still tests every tool he writes about on real servers.

Stop juggling tools. Start managing fleets.

PuTTY
Termius
MobaXterm
WinSCP
SecureCRT
Royal TS
Warp
CtrlOps14 tools left
Webmin
aaPanel
RunCloud
Dokploy
IP spreadsheet
bash (2)
sticky notes

One app. All your servers. No context switching. Free to start, free to leave.

Start instantly · No credit card · Cancel anytime

Your privacy preferences. We use product analytics to improve CtrlOps. Google Analytics cookies and live chat stay off until you allow them. Cookie policy