What is container resource limiting in Docker?
Why Interviewers Ask This
Mid-level Docker roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
Answer
Docker allows setting resource limits on containers to prevent any single container from consuming all host resources. Memory limits: docker run --memory 512m myapp — container cannot use more than 512MB RAM; --memory-swap 1g — total memory + swap (set equal to --memory to disable swap); without limits, one container can use all available RAM, causing OOM (Out of Memory) kills of other processes. CPU limits: docker run --cpus 1.5 myapp — container can use at most 1.5 CPU cores (out of available cores); --cpu-shares 512 — relative weight when CPUs are constrained (default 1024); --cpuset-cpus "0,1" — restrict to specific CPU cores. In Docker Compose: deploy: resources: limits: cpus: "0.5" memory: 512M reservations: cpus: "0.25" memory: 256M. Why limits matter: in Kubernetes, resource requests/limits are critical for scheduling and QoS; without limits, a buggy container (memory leak, infinite loop) degrades or crashes all co-located services. Monitoring: docker stats shows live CPU/memory usage. Observe memory usage in production and set limits ~20% above normal peak usage.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Docker codebase.
Previous
What is init in Docker containers?
Next
What is a Docker healthcheck and how does it differ from a liveness probe?