6.1.3.5 Stop Cleanup Decision
A focused guide to Stop Cleanup Decision, connecting core concepts with practical Docker and container operations.
The stop cleanup decision is the choice between simply stopping a container (preserving it for potential future restart) and stopping and removing it together, depending on whether the container's writable layer and configuration are still needed going forward.
Stopping Without Removing
Choosing to stop a container without removing it preserves its writable layer and configuration, appropriate when there's a reasonable chance the container will need to be restarted with that same state intact.
docker stop myapp
docker start myapp
This sequence resumes the container, including whatever state existed in its writable layer when it was stopped.
Stopping and Removing Together
For containers that are genuinely done being needed — a one-off task, a temporary debugging session — removing the container immediately after stopping avoids leaving behind an inert container that serves no further purpose.
docker run --rm myapp:1.0 some-one-off-command
The --rm flag automatically removes the container as soon as it stops, combining the stop and cleanup decision into the container's initial creation.
Why Leaving Stopped Containers Around Has a Cost
Stopped containers continue to consume disk space (their writable layer persists) and add clutter to container listings, even though they consume no CPU or memory while stopped — accumulating many unneeded stopped containers over time is a common, easily avoidable source of unnecessary disk usage.
docker ps -a --filter "status=exited"
docker container prune
Making This Decision Deliberately
Rather than defaulting to either always removing or never removing, deciding explicitly — based on whether a container's specific state genuinely needs to be preserved — leads to a cleaner, more intentional approach to managing the local container environment.
docker run --rm myapp:1.0 test-script.sh
docker run -d --name myapp myapp:1.0
Why the Stop Cleanup Decision Matters
Deliberately choosing whether to preserve or immediately discard a stopped container's state avoids both unintentionally accumulating unnecessary disk usage and unintentionally losing state that might still have been needed.