KatInterview

Completely Purge Docker Host Resources

System DesignModel answer
EasyModel answerSystem Designdockerdevopslinux

Last updated

Demonstrate how to safely and completely clear containers, images, volumes, custom networks, and build caches from a Docker daemon environment.

You need to write a procedure or script to completely reset a Docker host environment back to a clean state by removing all Docker resources.

Your solution must account for the full lifecycle of resources managed by the engine, including:

  1. Containers (both running and stopped)
  2. Images (including dangling, tagged, and intermediate layers)
  3. Volumes (both anonymous and named persistent volumes)
  4. Networks (user-defined custom networks while preserving Docker defaults)
  5. Build Cache (BuildKit objects)

Requirements:

  • Detail the precise sequence of operations required and explain why the sequence matters due to dependency graphs inside Docker.
  • Provide the complete set of CLI commands to accomplish this task, accounting for edge cases such as empty resource lists.
  • Explain the native shortcut commands (e.g., docker system prune), along with their technical caveats and why a raw system prune call alone might fall short.
  • Provide an executable script (e.g., Python or Bash) that wraps this workflow cleanly and safely.

Hints

Hint 1

Docker resources have strict dependency relationships: containers reference images, networks, and volumes.

Hint 2

Running containers must be stopped or forcefully removed before the resources they reference can be purged.

Hint 3

The native command `docker system prune` does not touch running containers and might preserve named volumes depending on flags and engine versions.