Docker Build: No Space Left on Device — How to Fix It
Getting 'no space left on device' during docker build? Here's exactly what's eating your disk and the commands to fix it fast without losing your images.
You're in the middle of a build and Docker hits you with:
error: failed to solve: failed to read dockerfile: error from sender:
write /tmp/docker-buildkit...: no space left on device
or
failed to create shim: OCI runtime create failed: container_linux.go:380:
starting container process caused: process_linux.go: no space left on device
Your disk is full. Here's how to figure out what's using it and clean it up without nuking anything important.
Step 1: Find Out How Much Docker Is Using
docker system dfOutput looks like:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 47 12 18.3GB 14.2GB (77%)
Containers 8 3 1.2GB 890MB (74%)
Local Volumes 15 6 4.5GB 2.1GB (46%)
Build Cache - - 6.8GB 6.8GB
This tells you exactly where the disk is going. In most cases, it's:
- Build cache — old build layers accumulating
- Images — pulled images you forgot about
- Stopped containers — exited containers still taking space
- Volumes — dangling volumes from deleted containers
Fix 1: Nuclear Option (When You're in a Hurry)
If you're in CI/CD or a dev machine where nothing important is running:
docker system prune -a --volumesWarning: This removes:
- All stopped containers
- All images not used by running containers
- All volumes not used by running containers
- All build cache
Frees up the most space. Use when you just need things to work NOW.
Fix 2: Safe Cleanup (Production/Shared Machines)
Do this step by step so you don't delete something you need.
Remove build cache first (safest)
# Remove all build cache
docker builder prune
# Remove only cache older than 48 hours
docker builder prune --filter until=48h
# Check how much space it used
docker builder prune --dry-runBuild cache is usually the biggest culprit and 100% safe to delete — Docker just rebuilds it next time.
Remove dangling images
# Dangling = untagged images (intermediate build layers)
docker image prune
# Check first
docker image prune --dry-runRemove specific images you don't need
# List all images with size
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h -r
# Remove specific image
docker rmi image-name:tag
# Remove all images with specific name pattern
docker images -q "myapp" | xargs docker rmiRemove stopped containers
# List stopped containers
docker ps -a --filter "status=exited"
# Remove them
docker container prune
# Or be selective
docker rm $(docker ps -aq --filter "status=exited")Remove dangling volumes
# List dangling volumes
docker volume ls -f dangling=true
# Remove them
docker volume prune
# Warning: volumes may contain database data!
# Check before deleting
docker volume inspect VOLUME_NAMEFix 3: Change Docker's Storage Location
If you're running out of space on / but have another disk with more space:
Linux (Docker daemon config):
# Edit daemon.json
sudo nano /etc/docker/daemon.json{
"data-root": "/mnt/data/docker"
}# Stop Docker, move data, restart
sudo systemctl stop docker
sudo mv /var/lib/docker /mnt/data/docker
sudo systemctl start dockerVerify:
docker info | grep "Docker Root Dir"Fix 4: BuildKit Cache Location
If you're using BuildKit (default in Docker 20+), it has its own cache location:
# Check BuildKit cache
du -sh ~/.docker/buildx
# Clean it
docker buildx prune
# Or for a specific builder
docker buildx prune --builder mybuilderFix 5: Docker in CI/CD (GitHub Actions, GitLab CI)
In CI environments, runners often run out of space because every build pulls images fresh.
GitHub Actions:
- name: Free up disk space
run: |
docker system prune -f
docker volume prune -f
- name: Build with BuildKit cache
uses: docker/build-push-action@v5
with:
context: .
push: true
cache-from: type=gha
cache-to: type=gha,mode=maxThe cache-from: type=gha uses GitHub Actions cache to persist Docker layers between runs, reducing both build time AND disk usage.
GitLab CI:
before_script:
- docker system prune -f --volumes
build:
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
after_script:
- docker system prune -fFix 6: Multi-stage Builds to Reduce Image Size
If space is a recurring problem, slim down your images so they don't fill disk in the first place:
# Before: 1.2GB image
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# After: ~200MB image with multi-stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]Set Up Automatic Cleanup
Don't wait until disk is full. Set up a cron job:
# Add to crontab (crontab -e)
# Run docker cleanup every Sunday at 2am
0 2 * * 0 docker system prune -f --filter "until=168h" >> /var/log/docker-cleanup.log 2>&1The --filter "until=168h" only removes things older than 7 days — safe for production.
Quick Summary
| What to delete | Command | Risk |
|---|---|---|
| Build cache | docker builder prune | None |
| Dangling images | docker image prune | None |
| Stopped containers | docker container prune | Low |
| Dangling volumes | docker volume prune | High (may have data) |
| Everything | docker system prune -a --volumes | High |
Always run with --dry-run first to see what will be deleted.
Docker Hub — use private repos and image retention policies to avoid accumulating old images in your registry too.
KodeKloud Docker Course — the best hands-on Docker training. Covers storage, networking, multi-stage builds, and production best practices.
Today I Fixed
Short real fixes from production — posted daily
Stay ahead of the curve
Get the latest DevOps, Kubernetes, AWS, and AI/ML guides delivered straight to your inbox. No spam — just practical engineering content.
Related Articles
Docker Daemon Not Starting on Linux — Fix Guide
Docker daemon fails to start, or the systemd service crashes immediately. Here's how to diagnose dockerd startup failures from storage driver conflicts to corrupted config files.
AWS ECR Image Push Access Denied — Every Fix (2026)
docker push to ECR fails with 'Access Denied' or 'no basic auth credentials'. Here's every cause — expired token, wrong region, missing IAM permissions, ECR URI mismatch — and the exact fix for each.
AWS ECS Task Keeps Stopping — How to Fix It (2026)
Your ECS task starts and then immediately stops or keeps restarting. Here's every reason this happens and how to debug and fix it.