Docker Disk Space Cleanup
Docker can consume gigabytes of disk space with unused images, containers, volumes, and build cache. This guide teaches you how to analyze disk usage and safely clean up unnecessary resources.
Docker stores data in several locations. The main storage location is `/var/lib/docker` on Linux. This directory contains images, containers, volumes, networks, and build cache. Over time, unused resources accumulate and consume disk space.
The first step in cleanup is understanding what's taking up space. Docker provides built-in commands to analyze disk usage by type.
# Show high-level disk usage
docker system df
# Show detailed breakdown of all resources
docker system df -v
# Check Docker directory size (Linux)
sudo du -sh /var/lib/docker
# Check specific directories
sudo du -sh /var/lib/docker/overlay2
sudo du -sh /var/lib/docker/volumes
The `docker system prune` command removes all stopped containers, unused networks, dangling images, and build cache. It's the quickest way to free up space.
# Remove stopped containers, unused networks, dangling images, build cache
docker system prune
# Remove all unused resources (including images without tags)
docker system prune -a
# Remove volumes as well (caution: data loss)
docker system prune -a --volumes
# Force without confirmation prompt
docker system prune -f
# Filter by time (remove resources older than 24 hours)
docker system prune --filter "until=24h"
# Remove a specific stopped container
docker rm container_name
# Force remove a running container
docker rm -f container_name
# Remove all stopped containers
docker container prune
# Remove all stopped containers (no prompt)
docker container prune -f
# Remove containers based on exit status
docker rm $(docker ps -a -q --filter "status=exited")
# Remove containers created more than 24 hours ago
docker container prune --filter "until=24h"
# Remove all containers (running and stopped) - DANGEROUS
docker rm -f $(docker ps -aq)
# Remove a specific image
docker rmi image_name:tag
# Remove force (if container using it)
docker rmi -f image_name
# Remove dangling images (untagged)
docker image prune
# Remove all unused images
docker image prune -a
# Remove images older than 24 hours
docker image prune -a --filter "until=24h"
# Remove images by pattern
docker images | grep "none" | awk '{print $3}' | xargs docker rmi
# Remove all images (DANGEROUS)
docker rmi -f $(docker images -q)
Volumes persist data even after containers are removed. They often take up the most space but also contain important data. Be careful when removing volumes.
# List volumes
docker volume ls
# Inspect volume
docker volume inspect volume_name
# Remove a specific volume (container must be stopped)
docker volume rm volume_name
# Remove all unused volumes
docker volume prune
# Force remove unused volumes
docker volume prune -f
# Remove all volumes (including used) - DANGEROUS
docker volume rm $(docker volume ls -q)
# Remove volumes not used by any container
docker volume prune --filter "label!=keep"
Docker build cache stores intermediate layers from image builds. Over time, this can consume significant disk space, especially in CI/CD environments.
# Remove build cache
docker builder prune
# Remove all build cache (no prompt)
docker builder prune -f
# Remove build cache older than 24 hours
docker builder prune --filter "until=24h"
# Remove all build cache (aggressive)
docker builder prune -a
# View build cache usage
docker system df
docker buildx du
# List networks
docker network ls
# Remove unused networks
docker network prune
# Remove specific network
docker network rm network_name
# Remove all unused networks
docker network prune -f
docker system prune -a
Remove all unused containers, networks, images (both dangling and unreferenced)
docker system prune -a --volumes
Remove everything + volumes (most aggressive)
docker container prune -f
Remove all stopped containers
docker image prune -a -f
Remove all unused images
docker volume prune -f
Remove all unused volumes
docker builder prune -a -f
Remove all build cache
Set up automatic cleanup to prevent disk space issues:
# Crontab for daily cleanup (Linux)
# Edit crontab: crontab -e
# Run daily at 2 AM
0 2 * * * docker system prune -f
# Run weekly, keep images from last 7 days
0 3 * * 0 docker image prune -a --filter "until=168h" -f
# Docker daemon configuration for log rotation (/etc/docker/daemon.json)
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
# Set image retention in CI/CD
# In GitHub Actions: add prune step after builds
- name: Clean up Docker
run: docker system prune -f
# Show top 10 largest images
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h -r | head -10
# Show containers with their sizes
docker ps -as
# Show volumes with their sizes
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock alpine/du:latest
# Find large files in Docker directory (Linux)
sudo find /var/lib/docker -type f -size +100M -exec ls -lh {} \;
# Show disk usage by Docker component
docker system df -v | grep -E "Images|Containers|Volumes|Build Cache"
- Run `docker system prune -a` weekly in CI/CD environments
- Configure log rotation in daemon.json to limit log sizes
- Use `--rm` flag for temporary containers
- Use multi-stage builds to reduce image sizes
- Set resource limits on containers to prevent runaway logs
- Monitor disk space with alerts
- Use `.dockerignore` to avoid sending unnecessary files
- Regularly review and remove old volumes
- Use named volumes for important data, anonymous volumes for temporary data
Regular disk space cleanup is essential for maintaining healthy Docker environments. Use these commands to reclaim gigabytes of storage and prevent disk full errors.