Skip to content

require-resource-limits

Docker Compose services without memory or CPU limits can starve the host. How to set deploy.resources.limits.

On this page

By default a Compose service can use every byte of memory and every CPU cycle the host has. One service with a memory leak doesn't just die — it drags the whole machine down first, taking every co-located service with it. This rule flags services that define no resource limits.

What the rule catches

compose.yaml — one leak can take down the host
services:
  api:
    image: acme/api:1.4.2

Scanning this file reports:

⚠ WARN [docker-doctor/require-resource-limits]
  Service 'api' does not have CPU or memory limits defined. A resource leak in this service could crash the host.

Why it matters

Limits are the isolation part of running containers. With a memory limit, a leaking service is OOM-killed at its cap and restarts — see require-restart-policy — while its neighbors keep serving; without one, the host OOM killer picks a victim, and it often picks wrong (your database, for instance). CPU limits likewise keep one busy-looping service from starving everything else. Setting limits also forces the question every production service should answer: how much is this supposed to use?

How to fix it

Add resource limits (e.g. deploy.resources.limits) to prevent a single service from starving host resources in production.

compose.yaml — the leak is contained at 512 MB
services:
  api:
    image: acme/api:1.4.2
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 512M

docker compose applies deploy.resources.limits directly (no Swarm required). Size limits from observed usage plus real headroom — a limit that's too tight turns normal traffic spikes into OOM kills. Note that Java and Node runtimes size their heaps from the container limit only in recent versions; older runtimes may need explicit flags (-Xmx, --max-old-space-size) to match.

The service-level mem_limit, cpus, and cpu_quota keys are the older spelling of the same limits and satisfy the rule too.

A one-shot job, such as a migration that another service waits on with condition: service_completed_successfully, still needs a limit. Only require-restart-policy exempts it.

A service with a provider: mapping is skipped. Compose hands it to a provider instead of running it as a container, so a limit has nothing to apply to.

Compose runs a base file merged with its override. When a directory holds a base file (compose.yaml, compose.yml, docker-compose.yml or docker-compose.yaml) and an override (compose.override.yml, compose.override.yaml, docker-compose.override.yml or docker-compose.override.yaml), this rule judges each service after the override merges over the base, the way docker compose does without -f. A setting in either file counts. When the base sets one and the override takes it away with !reset or !override, the finding lands on the override at that tag, because only the override can give it back. Any other finding lands on the base at the service's line, because that is where you add the setting. A service that only the override defines is reported on the override. Files passed with -f, extends and include are not followed.

Rule details

  • Rule key — docker-doctor/require-resource-limits
  • Category — Compose
  • Default severity — warning
  • Applies to — Docker Compose files

Explain this rule from the CLI:

npx @docker-doctor/cli@latest rules explain docker-doctor/require-resource-limits

Change its severity — or turn it off — in your config file:

// docker-doctor.config.ts
export default {
  rules: {
    "docker-doctor/require-resource-limits": "off",
  },
};

Severity affects the health score: error findings cost more points than warning, and info costs the least.