Sub topic 1
Why containers need resource limits
Prerequisites
This chapter assumes you can run basic Docker commands (docker run, docker ps, working with images) and use a Linux command line. You should also know roughly what CPU cores, RAM, and swap are. No earlier chapter is required.
Containers share the host
A container is not a virtual machine. Every container on a host uses the same Linux kernel, and so the same CPU cores and the same physical memory. Docker does not cap any of this by default. A container that gets no limits may use as much CPU time and memory as the host can give it.
That default is fine on a laptop with one small service. It becomes a problem when several workloads share a machine.
What goes wrong without limits
Three failure patterns show up repeatedly.
- Memory exhaustion. A container with a memory leak keeps growing. When the host runs out of RAM, the kernel's out-of-memory (OOM) killer picks a process to end. It may pick a process in a different container, or the Docker daemon itself, not the one that caused the problem.
- CPU starvation. A container running a tight loop or a heavy batch job can take most of the CPU time. Other containers on the host then respond slowly, even though nothing is wrong with them.
- Noisy neighbors. Two services that behave well alone can hurt each other when they peak at the same time. Without limits, you cannot predict which one will slow down.
What limits give you
Resource limits turn a shared machine into something you can plan around.
- Containment. A misbehaving container hits its own ceiling instead of the host's.
- Predictability. You can estimate how many containers fit on a host by adding up their limits.
- Clearer failures. A container killed for exceeding its memory limit leaves evidence (an exit code and a state flag), so you can find the cause quickly. Later chapters show how to read it.
- Fair sharing. You can give important services more CPU weight than background jobs.
A first look
You set limits with flags on docker run. This command starts a web server that can use at most 256 MiB of memory and half of one CPU core:
docker run -d --name web --memory 256m --cpus 0.5 nginx:alpineThe --memory flag sets a hard memory ceiling. The --cpus flag caps total CPU time at the equivalent of half a core. Docker enforces both through a Linux kernel feature called cgroups, which the next chapter explains.
Limits are not a substitute for sizing
Limits protect the host, but they do not fix an application that needs more than you gave it. A limit set too low causes OOM kills or slow responses. A limit set too high protects nothing. You will measure real usage and pick values deliberately in the final chapter.
Exercise
A container on a shared host has a memory leak and no memory limit. Name two ways this can affect other containers, and say how a memory limit changes the outcome.
Show answer
When the host runs out of RAM, the kernel's OOM killer may end a process in another container or the Docker daemon, and the host may slow down as it runs short of memory. With a memory limit, the leaking container reaches its own ceiling and is killed on its own, so other containers keep running.
Exercise
Which resource is slowed down when a container goes over its limit, and which one can cause the container to be killed?
Show answer
CPU is slowed down (throttled) when a container exceeds its limit. Memory can cause the container to be killed when it exceeds its hard limit.