What's Actually Happening
Your Docker build or image pull fails because the storage device is full. Docker stores images, containers, and volumes in /var/lib/docker (by default), and when this location runs out of space, all Docker operations fail. This commonly happens after building many images, running many containers, or accumulating orphaned resources.
The Error You'll See
```bash $ docker build -t my-app . ERRO[0012] failed to copy files: write /var/lib/docker/tmp/docker-builder123/build.tar: no space left on device
$ docker pull nginx failed to register layer: Error processing tar file(exit status 1): write /var/lib/docker/overlay2/.../root: no space left on device
$ docker run my-app docker: Error response from daemon: failed to create shim: OCI runtime create failed: no space left on device.
$ df -h /var/lib/docker Filesystem Size Used Avail Use% Mounted on /dev/sda1 100G 95G 0G 100% / ```
Why This Happens
- 1.Accumulated images: Old images not being cleaned up
- 2.Dangling images: Untagged images taking up space
- 3.Stopped containers: Exited containers still consuming space
- 4.Unused volumes: Orphaned volumes not deleted
- 5.Build cache: Docker build cache growing large
- 6.Log files: Container logs filling up disk
- 7.Overlay2 driver: Storage driver overhead
- 8.Small disk partition: Docker partition too small
Step 1: Check Disk Usage
First, identify what's consuming space:
```bash # Check overall disk usage df -h
# Check Docker disk usage summary docker system df
# Output example: TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 45 12 28.5GB 20.3GB (71%) Containers 8 3 1.2GB 800MB (66%) Local Volumes 15 5 5.8GB 4.2GB (72%) Build Cache 150 0 12.3GB 12.3GB (100%)
# Detailed breakdown docker system df -v
# Check /var/lib/docker size sudo du -sh /var/lib/docker sudo du -sh /var/lib/docker/* ```
Step 2: Quick Cleanup - Remove Unused Resources
Free up space quickly:
```bash # Remove all unused resources (images, containers, volumes, networks, build cache) docker system prune -a --volumes
# Warning: This removes everything not currently in use! # Type 'y' to confirm
# Less aggressive - only dangling resources docker system prune
# Remove only stopped containers docker container prune
# Remove only unused images docker image prune -a
# Remove only unused volumes docker volume prune
# Remove build cache docker builder prune ```
Step 3: Remove Specific Resources
Target specific items:
```bash # List all images docker images -a
# Remove specific image docker rmi <image-id>
# Remove multiple images docker rmi image1 image2 image3
# Force remove images (even if used by stopped containers) docker rmi -f <image-id>
# List all containers docker ps -a
# Remove specific container docker rm <container-id>
# Remove all stopped containers docker rm $(docker ps -a -q -f status=exited)
# List volumes docker volume ls
# Remove specific volume docker volume rm <volume-name>
# Find large files in Docker directory sudo find /var/lib/docker -type f -size +100M -exec ls -lh {} \; ```
Step 4: Clean Docker Build Cache
Build cache can grow very large:
```bash # Check build cache size docker system df -v | grep -A 20 "Build Cache"
# Remove all build cache docker builder prune -a
# Remove cache older than 24 hours docker builder prune --filter until=24h
# Remove cache for specific keep-storage docker builder prune --keep-storage 10GB ```
Step 5: Clean Container Logs
Container logs can fill disk space:
```bash # Check container log sizes sudo sh -c "du -h /var/lib/docker/containers/*/*-json.log | sort -hr | head -20"
# Truncate a specific log file sudo truncate -s 0 /var/lib/docker/containers/<container-id>/<container-id>-json.log
# Truncate all container logs sudo sh -c 'truncate -s 0 /var/lib/docker/containers/*/*-json.log'
# Configure log rotation for all containers # Create or edit /etc/docker/daemon.json sudo nano /etc/docker/daemon.json ```
Add log rotation configuration:
``json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Then restart Docker:
``bash
sudo systemctl restart docker
Step 6: Move Docker Data Directory
If disk partition is too small, move Docker to larger partition:
```bash # Stop Docker sudo systemctl stop docker
# Check current Docker root docker info | grep "Docker Root Dir"
# Create new location sudo mkdir -p /mnt/larger-disk/docker
# Copy Docker data sudo rsync -aP /var/lib/docker/ /mnt/larger-disk/docker/
# Backup original sudo mv /var/lib/docker /var/lib/docker.bak
# Create symlink sudo ln -s /mnt/larger-disk/docker /var/lib/docker
# Or configure daemon.json sudo nano /etc/docker/daemon.json ```
Add data directory:
``json
{
"data-root": "/mnt/larger-disk/docker"
}
```bash # Restart Docker sudo systemctl start docker
# Verify new location docker info | grep "Docker Root Dir" ```
Step 7: Use Overlay2 Storage Driver Efficiently
Ensure efficient storage driver:
```bash # Check current storage driver docker info | grep "Storage Driver"
# If using devicemapper, switch to overlay2 (more efficient) # Edit /etc/docker/daemon.json sudo nano /etc/docker/daemon.json ```
{
"storage-driver": "overlay2"
}# WARNING: This requires clearing existing Docker data
sudo systemctl stop docker
sudo rm -rf /var/lib/docker
sudo systemctl start dockerStep 8: Automate Cleanup
Set up automatic cleanup:
```bash # Create a cron job for automatic cleanup sudo crontab -e
# Add daily cleanup at 2 AM 0 2 * * * docker system prune -f --filter until=24h
# Or create a cleanup script cat > /usr/local/bin/docker-cleanup.sh << 'EOF' #!/bin/bash # Remove stopped containers docker container prune -f
# Remove unused images docker image prune -a -f --filter until=24h
# Remove unused volumes docker volume prune -f
# Clean build cache docker builder prune -f --filter until=24h
# Truncate logs larger than 100MB find /var/lib/docker/containers -name "*-json.log" -size +100M -exec truncate -s 0 {} \; EOF
chmod +x /usr/local/bin/docker-cleanup.sh
# Add to cron echo "0 2 * * * /usr/local/bin/docker-cleanup.sh" | sudo crontab - ```
Verify the Fix
Confirm space is available and Docker works:
```bash # Check available space df -h /var/lib/docker
# Should show available space # Example: Use% should be below 90%
# Verify Docker disk usage docker system df
# Should show reclaimable space reduced
# Test build docker build -t test-image .
# Test pull docker pull hello-world
# Test run docker run hello-world ```
You should now have sufficient disk space and Docker operations should complete successfully.