🔧 Error Fixes

Docker: No Space Left on Device — How to Free Up Disk Space


no space left on device
Error response from daemon: failed to mkdir: no space left on device
write /var/lib/docker/...: no space left on device

Docker is eating your disk. Images, containers, build cache, and volumes accumulate over time. Here’s how to clean up.

Step 1: See What’s Using Space

docker system df

This shows disk usage by images, containers, volumes, and build cache.

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          45        3         12.5GB    11.2GB (89%)
Containers      12        1         500MB     480MB (96%)
Local Volumes   8         2         3.2GB     2.8GB (87%)
Build Cache     120       0         5.1GB     5.1GB (100%)

That “RECLAIMABLE” column is what you can free up.

Fix 1: The Nuclear Option (Clean Everything Unused)

# Remove all unused containers, networks, images, and build cache
docker system prune

# Also remove unused volumes (⚠️ deletes data!)
docker system prune --volumes

# Remove EVERYTHING unused, including images not used by running containers
docker system prune -a

# The full cleanup
docker system prune -a --volumes

⚠️ --volumes deletes volume data. Make sure you don’t need it.

Fix 2: Clean Up Selectively

# Remove stopped containers
docker container prune

# Remove unused images
docker image prune        # only dangling (untagged)
docker image prune -a     # all unused images

# Remove unused volumes
docker volume prune

# Remove build cache
docker builder prune
docker builder prune -a   # remove all build cache

Fix 3: Remove Specific Items

# Remove specific images
docker rmi image_name:tag
docker rmi $(docker images -q --filter "dangling=true")

# Remove all containers
docker rm $(docker ps -aq)

# Remove images older than 24 hours
docker image prune -a --filter "until=24h"

Fix 4: Reduce Image Sizes

Your Dockerfiles might be creating unnecessarily large images.

# ❌ Large image (1GB+)
FROM node:20
COPY . .
RUN npm install
CMD ["node", "app.js"]

# ✅ Smaller image (~150MB)
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["node", "app.js"]

Use .dockerignore to exclude files from the build context:

node_modules
.git
*.md
.env
dist

Fix 5: Increase Docker’s Disk Allocation

Docker Desktop (Mac/Windows):

  1. Open Docker Desktop → Settings → Resources
  2. Increase “Disk image size”
  3. Apply & Restart

Linux: Docker uses /var/lib/docker by default. Move it to a larger disk:

sudo systemctl stop docker
sudo mv /var/lib/docker /new-disk/docker
sudo ln -s /new-disk/docker /var/lib/docker
sudo systemctl start docker

Automate Cleanup

Add a cron job to clean up weekly:

# Add to crontab (crontab -e)
0 3 * * 0 docker system prune -af --volumes 2>&1 | logger -t docker-cleanup

Or add to your CI pipeline:

# Clean up before builds
- run: docker system prune -af

Prevention

  • Use multi-stage builds to keep images small
  • Always use .dockerignore
  • Use --rm flag when running temporary containers: docker run --rm ...
  • Run docker system df monthly to check disk usage
  • Use alpine base images when possible