Container Resource Limits and Health Checks
You'll learn how to constrain container resource usage, configure health monitoring, and set restart policies to keep your containerized applications stable and predictable.
Why Resource Limits Matter
Without constraints, a single container can consume all available CPU or memory on a host, starving other containers. Think of it like roommates sharing utilities—without agreements on usage, one person's Bitcoin mining operation could drive up everyone's electricity bill and slow the internet to a crawl.
Setting Resource Limits
You define CPU and memory constraints when running containers to prevent resource monopolization:
- Memory limits: Cap the maximum RAM a container can use (e.g., 512MB). If exceeded, the container gets killed.
- CPU limits: Restrict CPU shares or cores (e.g., 0.5 CPUs means half a core's worth of time).
These limits ensure fair resource distribution and prevent cascading failures where one misbehaving container impacts the entire system.
Health Checks: Is Your Container Really Working?
A running container isn't necessarily a healthy container. Your application might be deadlocked or unable to serve requests despite the process still existing. Health checks probe your application periodically:
- Liveness probes: "Is the app alive?" If it fails repeatedly, restart the container.
- Readiness probes: "Can the app handle traffic?" Routes traffic only when healthy.
For example, an HTTP endpoint returning 200 OK every 10 seconds confirms your web service is responsive.
Restart Policies
Define what happens when containers fail:
no: Never restart automaticallyon-failure: Restart only if the container exits with an erroralways: Always restart, regardless of exit statusunless-stopped: Always restart unless manually stopped
Key Takeaway: Resource limits prevent containers from monopolizing host resources, health checks ensure your application is genuinely functional (not just running), and restart policies automate recovery—together, they form the foundation of reliable containerized systems.