0
0
Dockerdevops~5 mins

Container disk usage management in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Containers can use a lot of disk space over time, which can slow down your system or fill up your storage. Managing container disk usage helps keep your system clean and running smoothly by removing unused data.
When your system storage is running low due to leftover container data
When you want to free up space by deleting stopped containers and unused images
When you want to check how much disk space your containers and images are using
When you want to clean up unused volumes that take up space
When you want to maintain a healthy environment by regularly removing unused Docker resources
Commands
This command shows how much disk space is used by Docker images, containers, volumes, and build cache. It helps you understand where your disk space is going.
Terminal
docker system df
Expected OutputExpected
TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 5 3 2.5GB 1.2GB (48%) Containers 4 2 500MB 300MB (60%) Local Volumes 3 2 1.1GB 700MB (63%) Build Cache 0 0 0B 0B
This command removes all stopped containers to free up disk space. The -f flag skips the confirmation prompt for faster cleanup.
Terminal
docker container prune -f
Expected OutputExpected
Deleted Containers: 8a7f3c2d4b1e b3c9d7a1e2f4 Total reclaimed space: 300MB
-f - Force removal without confirmation prompt
This command removes all unused images, including dangling and unreferenced images, to free up disk space. The -a flag means all unused images, and -f skips confirmation.
Terminal
docker image prune -a -f
Expected OutputExpected
Deleted Images: sha256:abc123def456 sha256:def789abc012 Total reclaimed space: 1.2GB
-a - Remove all unused images, not just dangling ones
-f - Force removal without confirmation prompt
This command removes all unused volumes that are not referenced by any container, freeing up disk space. The -f flag skips confirmation.
Terminal
docker volume prune -f
Expected OutputExpected
Deleted Volumes: my_unused_volume Total reclaimed space: 700MB
-f - Force removal without confirmation prompt
Key Concept

If you remember nothing else from this pattern, remember: regularly check and clean unused Docker resources to keep your disk space healthy.

Common Mistakes
Running prune commands without the -f flag and expecting no prompt
The command will pause and wait for confirmation, which can interrupt automated scripts or workflows.
Use the -f flag to force removal without confirmation when you want non-interactive cleanup.
Removing images without understanding which are in use
You might delete images that are still needed by running containers, causing errors or downtime.
Check active containers and images with 'docker system df' before pruning images.
Ignoring volumes when cleaning up
Unused volumes can take up significant disk space but are not removed by container or image pruning.
Use 'docker volume prune' to clean up unused volumes regularly.
Summary
Use 'docker system df' to see disk usage by Docker resources.
Remove stopped containers with 'docker container prune -f' to free space.
Clean unused images with 'docker image prune -a -f' safely.
Remove unused volumes using 'docker volume prune -f' to reclaim disk space.