Course contentsShow
← Cloud and Infrastructure
Lesson 964 of 2,98919. Container FundamentalsFree lesson

Resource Limits with cgroups

Using control groups (cgroups) to limit CPU, memory, and I/O resources for containers.

Resource Limits with cgroups

You'll learn how control groups (cgroups) let you cap CPU, memory, and I/O usage for containers so they can't hog your system.

What Are cgroups?

Control groups, or cgroups, are a Linux kernel feature that put guardrails around resource consumption. While namespaces (which you just learned) isolate what a process can see, cgroups limit what resources it can actually use.

Think of it like giving each container a budget: "You get 512 MB of RAM, 50% of one CPU core, and 10 MB/s of disk I/O—no more." Without cgroups, a runaway container could consume all available memory or CPU, starving other containers or crashing the host.

Why This Matters for Containers

Containers share the host kernel and hardware. Without limits, one misbehaving container could:

  • Eat all the RAM, causing the system to swap or kill processes
  • Pin the CPU at 100%, making everything sluggish
  • Flood disk I/O, slowing down all storage operations

Cgroups prevent these "noisy neighbor" problems by enforcing hard or soft resource caps per container.

What You Can Control

  • Memory: Set maximum RAM usage (e.g., 1 GB cap)
  • CPU: Limit CPU time or assign shares (e.g., "use 25% of one core")
  • Block I/O: Throttle read/write speeds to disk
  • Network I/O: Control bandwidth (with additional tools)

Container runtimes like Docker or containerd configure cgroups automatically when you specify resource limits—you don't usually write cgroup rules by hand.

Key Takeaway:
cgroups enforce resource limits (CPU, memory, I/O) on containers, preventing any single container from monopolizing the host's hardware.