Docker Compose CPU usage limit missing

Set a CPU limit suited to the service’s workload.

Description

A CPU reservation sets aside capacity for the service. The container can use more unless you also set a limit.

Potential impact

A busy service can slow or prevent responses from other services on the same host.

Remediation

Set a CPU limit your runtime supports, such as deploy.resources.limits.cpus. Verify that your platform honors both the reservation and the limit, then load-test it against your throughput needs.

Examples

Both examples request a reservation of 0.1 CPU. The second adds a 0.3-CPU limit and a 128M memory reservation. Replace example/app with your image.

Before

yaml
version: "3.7"
services:
  app:
    image: example/app
    deploy:
      resources:
        limits:
          memory: 256M
        reservations:
          cpus: "0.1"

After

yaml
version: "3.7"
services:
  app:
    image: example/app
    deploy:
      resources:
        limits:
          cpus: "0.3"
          memory: 256M
        reservations:
          cpus: "0.1"
          memory: 128M

References