Your VPS has been running a handful of containers for a few months. Nothing changed, nobody uploaded anything big, and yet df -h says the root disk is at 97%. Deploys start failing with no space left on device, MySQL inside a container refuses to write, and the culprit turns out to be one folder: /var/lib/docker.
This happens on almost every Docker host eventually. Docker is great at creating things and terrible at cleaning up after itself. Here's how to find exactly what's eating the space, clear it safely, and stop it from coming back.
Symptoms You'll Notice
docker pullordocker buildfails withwrite /var/lib/docker/...: no space left on device- Containers crash or restart in a loop because they can't write logs or temp files
du -sh /var/lib/dockershows tens of gigabytes, far more than your images should need- Other services on the VPS (SSH logins, cron, databases) start misbehaving because
/is full
Step 1: Confirm Docker Is the Problem
Start with the basics so you're not chasing the wrong thing:
df -h /
sudo du -sh /var/lib/docker
sudo du -sh /var/lib/docker/* 2>/dev/null | sort -h
If /var/lib/docker accounts for most of the used space, keep going. Now ask Docker for its own breakdown:
docker system df
You'll get four rows: Images, Containers, Local Volumes and Build Cache, each with a RECLAIMABLE column. That column is the space Docker thinks it can free without touching anything currently in use. For per-item detail, run docker system df -v.
One thing docker system df does not show clearly is container log files, which are often the biggest offender. We'll get to those in Step 3.
Why Docker Fills Your Disk
| Cause | Where it lives | How it builds up |
|---|---|---|
| Old and dangling images | /var/lib/docker/overlay2 | Every rebuild or pull of a new tag leaves the previous image behind |
| Stopped containers | /var/lib/docker/containers + overlay2 | Containers run without --rm stick around after they exit |
| Unused volumes | /var/lib/docker/volumes | Removing a container doesn't remove its volumes |
| Build cache | BuildKit cache in overlay2 | Each docker build keeps intermediate layers for speed |
| Container logs | /var/lib/docker/containers/<id>/<id>-json.log | The default json-file log driver has no size limit |
Step 2: Prune What You Don't Need
Work from the safest command to the most aggressive. Read what each one removes before you hit Enter.
Stopped containers
docker container prune
Removes every container that isn't running. If you keep a stopped container around on purpose (a one-off migration job you plan to re-run, for example), check docker ps -a first.
Dangling and unused images
docker image prune # only dangling (untagged) layers
docker image prune -a # every image not used by a container
The -a version is where the big wins usually are. It's safe in the sense that any image you need can be pulled again, but it means your next deploy will download everything fresh. To keep recent images, add a filter:
docker image prune -a --filter "until=168h"
That only removes unused images created more than 7 days ago.
Build cache
docker builder prune
docker builder prune -a # remove all build cache, not just dangling
On a VPS that builds images on every deploy, the build cache alone can hit 10–20 GB.
Volumes (be careful)
docker volume ls
docker volume prune
Volumes are where your databases and uploads live. On Docker Engine 23 and newer, docker volume prune only removes unused anonymous volumes; adding -a also removes unused named volumes. Never run it with -a unless you've confirmed with docker volume ls that nothing important is unattached. A stopped database container's volume counts as "unused" if the container has been removed.
The all-in-one command
docker system prune -a
This removes stopped containers, unused networks, all unused images and build cache. It does not touch volumes unless you add --volumes, and I'd recommend leaving that flag off.
Step 3: Find and Shrink Giant Container Logs
This is the one that catches people out. A chatty app writing to stdout can produce a multi-gigabyte log file in a few weeks, and none of the prune commands touch logs of running containers. Find them:
sudo sh -c 'du -sh /var/lib/docker/containers/*/*-json.log' | sort -h | tail -5
To match a log file to a container, take the long ID from the path and run docker ps --no-trunc | grep <first-12-chars>.
To free the space immediately without stopping the container, truncate the file rather than deleting it:
sudo truncate -s 0 /var/lib/docker/containers/<id>/<id>-json.log
Don't rm it. Docker still has the file open, so deleting it won't free the space until the container restarts, and it can confuse docker logs.
Step 4: Cap Log Size Permanently
Truncating is a one-time fix. To stop logs from growing forever, set a global limit in /etc/docker/daemon.json (create the file if it doesn't exist):
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
That keeps at most three 10 MB files per container. Validate the JSON and restart Docker:
python3 -m json.tool /etc/docker/daemon.json
sudo systemctl restart docker
Restarting Docker briefly stops your containers (those with a restart policy come back on their own). The important catch: log settings only apply to containers created after the change. Existing containers keep their old unlimited config until you recreate them:
docker compose up -d --force-recreate
You can also set limits per service in docker-compose.yml:
services:
app:
image: myapp:latest
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
Step 5: Things You Should Never Do
- Don't delete folders inside
/var/lib/docker/overlay2by hand. Docker tracks layers in its own metadata, and removing directories manually corrupts images and containers. Always go throughpruneordocker rmi. - Don't run
docker system prune -a --volumeson a production box without a fresh database backup. - Don't
rm -rf /var/lib/dockerto "start fresh" unless you genuinely want to lose every image, container and volume.
Prevention: Keep Docker Disk Usage in Check
- Schedule a weekly prune. Add this to root's crontab with
sudo crontab -e:
It clears unused images, containers and build cache older than a week, and leaves volumes alone.0 3 * * 0 /usr/bin/docker system prune -af --filter "until=168h" > /dev/null 2>&1 - Use
--rmfor one-off containers, likedocker run --rm alpine ..., so they delete themselves on exit. - Use multi-stage builds and a
.dockerignoreso images stay small and build context doesn't drag innode_modulesor.git. - Move Docker to a bigger disk if you attach extra storage. Set
"data-root": "/mnt/data/docker"indaemon.json, stop Docker, copy the data withsudo rsync -aP /var/lib/docker/ /mnt/data/docker/, then start Docker again. - Alert before it's full. A simple monitoring tool like Netdata, or a cron script that checks
df, warns you at 80% instead of finding out at 100%.
If your SkyServer VPS keeps running out of room even after cleanup, it may simply be undersized for your workload. Resizing the disk is usually quicker than fighting for every gigabyte.
Frequently Asked Questions
Is docker system prune -a safe to run?
It won't touch running containers, their images, or any volumes (unless you add --volumes). The main side effect is that removed images have to be downloaded again on the next deploy, and stopped containers you wanted to keep are gone.
Why didn't pruning free much space?
Usually because the space is in container log files, which prune doesn't touch, or in volumes. Check log sizes with the du command in Step 3 and volumes with docker system df -v.
Will capping logs in daemon.json affect my running containers?
No. The new limits only apply to containers created after Docker restarts. Recreate existing ones with docker compose up -d --force-recreate to apply them.
Can I delete the overlay2 folder to get space back?
No. Deleting anything under overlay2 manually breaks Docker's layer tracking and can leave containers that won't start. Use the prune commands instead.
How much free space should a Docker VPS keep?
Aim for at least 20% free. Image pulls and builds need temporary space, and a disk that's 95% full can fail a deploy halfway through.
