Docker Compose check · low

Services without resource limits

By default a container has no resource constraints and can use as much memory and CPU as the host gives it. One leaking or overloaded service can then slow or crash every other service on the machine. The explainer flags, as low, services with none of mem_limit, cpus or deploy.resources.limits.

$ explain docker-compose.yml
! app has no resource limits low
line 2
fix: see below
$ 

What the free Docker Compose explainer reports on the risky example below.

Why it matters

Docker's documentation states that a container has no resource constraints unless you set them. A memory leak in one service grows until the host runs out.

When the host runs out of memory, the kernel kills processes to recover, and it does not necessarily pick the one that caused the problem.

A limit turns a host-wide outage into a single service that is restarted, which its restart policy and healthchecks can handle.

Risky and safer docker-compose.yml

Riskydocker-compose.yml
services:
  app:
    image: example/app:1.4.2
    restart: unless-stopped
Saferdocker-compose.yml
services:
  app:
    image: example/app:1.4.2
    restart: unless-stopped
    mem_limit: 512m
    cpus: 1.0

How to fix it

  • Measure what the service normally uses, for example with docker stats, and set mem_limit with some headroom above it.
  • Set cpus to keep one busy service from monopolising the CPU.
  • If you prefer the deploy section, deploy.resources.limits with memory and cpus works too.

When it's acceptable

  • A machine that runs a single service, where there is nothing else to protect.
  • Short local runs where you want to see the service's unconstrained behaviour.

Check your own docker-compose.yml

The risky example is loaded below. Paste your own file to check it for this and every other Docker Compose rule. It runs in your browser; nothing is uploaded.

docker-compose.yml

Parsed in your browser. Nothing is uploaded.

Explanation

1 service0 networks0 volumes1 low

Top level

appexample/app:1.4.2

Also worth a look

A leak or a burst of traffic can consume all memory or CPU on the host and take neighbouring services down.

Safer: Set mem_limit and cpus, or deploy.resources.limits.

Questions

Does Docker limit container memory by default?

No. Without a limit a container can use as much memory as the host kernel allows.

Should I use mem_limit or deploy.resources.limits?

Either works with Docker Compose, and the explainer accepts both. Pick one style and use it across the file.

What happens when a container hits its memory limit?

The kernel kills a process inside that container. With a restart policy, Docker then restarts it, and the rest of the host is unaffected.