0 / 11 lessons — 0%
Lesson 09 / 11
Container lifecycle, restarts & limits
A container moves through a small set of states, and most confusion ("why is it not running, I just started it!") comes from not knowing which one you're looking at.
created → running → exited → removed. A restart policy can send it straight back to running.
# restart policies — decide what happens when a container exits docker run -d --restart unless-stopped myapp:1.0 docker run -d --restart always myapp:1.0 docker run -d --restart on-failure:5 myapp:1.0
| Policy | Behavior |
|---|---|
no | default — never restart automatically |
on-failure[:N] | restart only on a non-zero exit code, up to N times |
always | restart no matter what, even after a reboot |
unless-stopped | like always, but stays stopped if you stopped it manually |
Left unchecked, a container can eat every CPU core and every byte of RAM on the box. Cap it:
docker run -d --memory=512m --cpus=1.5 myapp:1.0 # live resource usage, like `top` but for containers docker stats
Try it yourselfRun
docker stats while a container is under load (even just docker run -it --rm progrium/stress --cpu 2). Watching CPU/memory numbers move in real time makes "resource limits" click instantly.