Docker resource limits

Topic: Containers core

Summary

Limit container CPU and memory with --cpus and --memory. Prevents one container from starving others. Set in docker run or compose. Use when running multiple containers on one host.

Intent: How-to

Quick answer

  • docker run --memory=512m --cpus=0.5 image. Limits RAM and CPU.
  • In compose use deploy.resources.limits. Set reservations for minimum.
  • OOM when over memory. Check docker stats.

Prerequisites

Steps

  1. Set in run

    docker run -d --memory=512m --cpus=0.5 myimage.

  2. Set in compose

    deploy.resources.limits.memory and cpus in service.

  3. Verify

    docker stats shows limit. Stress test to confirm cap.

Summary

Set —memory and —cpus to cap usage. Verify with docker stats.

Prerequisites

Steps

Step 1: Set in run

Use —memory and —cpus in docker run.

Step 2: Set in compose

Set deploy.resources.limits in compose.

Step 3: Verify

docker stats; stress test.

Verification

  • Limits show in docker stats; container cannot exceed.

Troubleshooting

OOM — Raise limit or fix leak. Throttled — Raise CPU limit.

Next steps

Continue to