# Docker No Space Left on Device: Complete Cleanup Guide

You're pulling an image, building a project, or running a container, and Docker throws "no space left on device". The error might appear during image pulls, container writes, or builds. Docker consumes disk space rapidly, and without regular cleanup, you'll hit this error eventually.

Common error messages:

bash
failed to register layer: Error processing tar file: no space left on device
bash
Error: write /var/lib/docker/tmp/...: no space left on device
bash
failed to create overlayfs metacopy: no space left on device

Quick Diagnosis

Check how much space Docker is using:

bash
docker system df

Output shows: `` TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 45 12 12.5GB 8.2GB (65%) Containers 23 3 1.2GB 1.1GB (91%) Local Volumes 8 4 500MB 250MB (50%) Build Cache 102 0 5.8GB 5.8GB (100%)

Check system disk space:

bash
df -h /var/lib/docker

Find Docker's root directory:

bash
docker info | grep "Docker Root Dir"

Cleanup Strategies

Strategy 1: Quick Cleanup (Safe)

Remove unused resources that aren't needed:

bash
docker system prune

This removes: - Stopped containers - Unused networks - Dangling images (images not referenced by any container) - Build cache

Warning: This does NOT remove: - Running containers - Images used by stopped containers - Named volumes

Strategy 2: Aggressive Cleanup (Careful)

Remove everything not currently in use:

bash
docker system prune -a --volumes

This removes: - All stopped containers - All unused images (not just dangling) - All unused volumes - All unused networks - All build cache

Warning: This will remove images you might need later. You'll have to re-pull them.

Strategy 3: Targeted Cleanup

Clean specific resource types.

Remove unused images: ```bash # Remove dangling images (not tagged, not used) docker image prune

# Remove all unused images docker image prune -a

# Remove specific image docker rmi <image_name>

# Force remove docker rmi -f <image_name> ```

Remove stopped containers: ```bash # Remove all stopped containers docker container prune

# Remove specific containers docker rm <container_name>

# Remove all containers (including running) docker rm -f $(docker ps -aq) ```

Remove unused volumes: ```bash # Remove unused volumes docker volume prune

# Remove specific volume docker volume rm <volume_name>

# List volumes with size docker volume ls -q | xargs docker volume inspect --format '{{ .Name }}: {{ .Mountpoint }}' ```

Remove unused networks: ``bash docker network prune

Clean build cache: ```bash # Remove all build cache docker builder prune -a

# Remove cache older than 24 hours docker builder prune --filter "until=24h" ```

Strategy 4: Cleanup by Size

Find and remove large items:

```bash # List images by size docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h

# Remove largest images docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}" --filter "dangling=false" | head -10) ```

List containers by size:

bash
docker ps -as --format "table {{.Names}}\t{{.Size}}" | sort -k2 -h

Strategy 5: Cleanup with Filters

Remove items created before a certain time:

```bash # Remove containers stopped more than 24 hours ago docker container prune --filter "until=24h"

# Remove images created more than 7 days ago docker image prune -a --filter "until=168h"

# Remove build cache older than 1 week docker builder prune --filter "until=168h" ```

Remove items with labels:

```bash # Remove containers with specific label docker rm $(docker ps -a --filter "label=environment=test" -q)

# Remove images with specific label docker rmi $(docker images --filter "label=environment=dev" -q) ```

Deep Cleanup

Remove Dangling Everything

Dangling resources are unreferenced layers:

```bash # Remove dangling images docker rmi $(docker images -f "dangling=true" -q)

# Remove dangling volumes docker volume rm $(docker volume ls -f "dangling=true" -q) ```

Clean Up Container Logs

Container logs can consume massive space:

```bash # Find log files find /var/lib/docker/containers -name "*-json.log" -exec ls -lh {} \;

# Truncate log files (stops containers first) truncate -s 0 /var/lib/docker/containers/*/*-json.log ```

Better: Configure log rotation in /etc/docker/daemon.json:

json
{
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Clean Up Overlay2

The overlay2 storage driver stores layers:

```bash # Check overlay2 size du -sh /var/lib/docker/overlay2

# This is managed by Docker; don't delete manually # Run docker system prune instead ```

Move Docker to Larger Disk

If your root disk is full, move Docker:

```bash # Stop Docker sudo systemctl stop docker

# Copy data sudo rsync -avxP /var/lib/docker/ /mnt/larger-disk/docker/

# Update Docker configuration # Edit /etc/docker/daemon.json { "data-root": "/mnt/larger-disk/docker" }

# Start Docker sudo systemctl start docker ```

Clean Up BuildKit Cache

If using BuildKit:

```bash # Show build cache docker buildx du

# Clean build cache docker buildx prune

# Clean aggressively docker buildx prune -a ```

Automated Cleanup

Cron Job

Set up automatic cleanup:

```bash # Edit crontab crontab -e

# Add daily cleanup at 2 AM 0 2 * * * docker system prune -f > /var/log/docker-cleanup.log 2>&1 ```

Docker Configuration

Prevent accumulation in /etc/docker/daemon.json:

json
{
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "storage-opts": [
    "overlay2.size=20G"
  ]
}

Restart Docker after changes:

bash
sudo systemctl restart docker

Docker Compose Auto-Cleanup

Use --rm flags:

```bash # Remove containers after they exit docker-compose up --rm

# Remove containers after running docker run --rm <image> ```

Verification Steps

After cleanup, verify space is reclaimed:

```bash # Check Docker space docker system df

# Check system space df -h /var/lib/docker

# Verify Docker still works docker run --rm hello-world ```

Prevention Tips

  1. 1.Use minimal base images:
  2. 2.```dockerfile
  3. 3.# Instead of ubuntu:latest
  4. 4.FROM alpine:3.18
  5. 5.# or
  6. 6.FROM distroless/base
  7. 7.`
  8. 8.Multi-stage builds:
  9. 9.```dockerfile
  10. 10.# Build stage
  11. 11.FROM golang:1.21 AS builder
  12. 12.WORKDIR /app
  13. 13.COPY . .
  14. 14.RUN go build -o myapp

# Runtime stage FROM alpine:3.18 COPY --from=builder /app/myapp /usr/local/bin/ CMD ["myapp"] ```

  1. 1.Regular cleanup schedule:
  2. 2.```bash
  3. 3.# Weekly cleanup
  4. 4.docker system prune -f
  5. 5.docker volume prune -f
  6. 6.docker builder prune -f
  7. 7.`
  8. 8.Limit container resources:
  9. 9.```bash
  10. 10.docker run --memory=512m --cpus=1 <image>
  11. 11.`
  12. 12.Set log limits:
  13. 13.```bash
  14. 14.docker run --log-opt max-size=10m --log-opt max-file=3 <image>
  15. 15.`

Troubleshooting Specific Scenarios

Image Pull Fails

bash
failed to register layer: Error processing tar file: no space left on device

Fix: ```bash # Clean up space first docker system prune -a

# Then pull docker pull <image> ```

Build Fails

bash
failed to create overlayfs: no space left on device

Fix: ```bash # Clean build cache docker builder prune -a

# Try building again docker build -t myapp . ```

Container Write Fails

bash
write error: no space left on device

Fix: ```bash # Check container writable layer docker ps -as

# Remove large containers docker rm <container>

# Or clean container data docker container prune ```

Regular cleanup prevents "no space left" errors. Set up automated cleanup and monitor disk usage to catch issues before they block your work.