Docker build fails with 'no space left on device' on a GitHub Actions runner
Our self-hosted runner is a Ubuntu 22.04 box with a 200GB root disk, shared by three repos. It started failing builds on a Wednesday with an error that blamed npm, which was misleading enough that I spent an hour looking at package versions.
## The error in the build log
=> [stage-0 3/6] RUN npm ci
#9 3.812 npm WARN deprecated inflight@1.0.6: This module is not supported, and leaks memory.
#9 5.441 npm ERR! code ENOSPC
#9 5.442 npm ERR! syscall open
#9 5.443 npm ERR! path /app/node_modules/@babel/core/lib/config/files/module-types.js
#9 5.444 npm ERR! errno -28
#9 5.445 npm ERR! nospc ENOSPC: no space left on device, open '/app/node_modules/@babel/core/lib/config/files/module-types.js'
#9 5.446 npm ERR! nospc There appears to be not enough free space.
------
failed to solve: process "/bin/sh -c npm ci" did not complete successfully: exit code: 1
ENOSPC inside the container almost always means the host is full, not the npm cache. The container sees the host filesystem through overlay2.
## Confirming where it went
$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/nvme0n1p1 196G 193G 1.2G 99% /$ sudo du -sh /var/lib/docker/* 2>/dev/null | sort -h
4.1G /var/lib/docker/containers
11G /var/lib/docker/image
178G /var/lib/docker/buildkit
178GB of BuildKit cache. Builds had been running for months and nothing ever pruned it. Each docker/build-push-action step was keeping old layers for every commit, including rebuilds of the same SHA.
## Getting unstuck
The safe version removes only unused cache, not images that running containers need:
docker builder prune -f --filter 'until=72h'
docker image prune -f --filter 'until=168h'
That freed about 140GB and the next build went through. I would not start with docker system prune -af on a self-hosted runner; we have had jobs fail because a cached base image vanished and a private registry rate-limited the re-pull.
The version-aware bit: this runner had Docker Engine 25.0.3 with BuildKit enabled by default. On Docker 23 and earlier the cache lived under /var/lib/docker/buildkit too, but the prune filters behaved differently, so check docker version before copying commands from an old post.
## Making it stop happening
I added a systemd timer that runs the filtered prune weekly, and capped the cache in /etc/docker/daemon.json:
{
"builder": {
"gc": {
"enabled": true,
"defaultKeepStorage": "40GB",
"policy": [
{ "keepStorage": "40GB", "filter": ["unused-for=168h"] }
]
}
}
}
Then sudo systemctl restart docker. The defaultKeepStorage setting is the one that actually matters; without it BuildKit will keep growing until the disk is full regardless of your cron job.
## The annoying part
GitHub-hosted runners do not have this problem because they are thrown away after each job. We moved to self-hosted for a private registry and faster builds, and quietly took on all the maintenance that comes with it. If your builds are not actually faster, I would reconsider. Ours are about 40 seconds quicker, which is nice, but I have spent more than that maintaining the box this month.