How to Limit Docker Container Disk Usage Before It Fills Your VPS

Docker makes it easy to ship an app, and just as easy to run out of disk without noticing. Images pile up, containers write logs forever, and a single runaway volume can eat a 40 GB VPS disk in a weekend. The first sign is usually MySQL refusing to start or a deploy failing with “no space left on device”. Learning how to limit Docker container disk usage before that happens is cheaper than cleaning up after it, because a full root partition can take down every service on the box at once.

This guide covers the three layers that matter: hard limits on what a container can write, log rotation so logs stop growing without bound, and a prune routine that clears the junk Docker leaves behind. None of this needs a paid tool, and all of it works on a plain Ubuntu VPS.

Find out where the space is going first

Before you set any limit, measure. Docker ships with a built-in report for this:

docker system df
docker system df -v

The first command shows totals for images, containers, local volumes, and the build cache, plus how much of it is reclaimable. The verbose flag breaks it down per item, so you can see whether one image or one volume is responsible. Pair it with a plain disk check:

df -h /
du -sh /var/lib/docker/* 2>/dev/null | sort -rh | head -10

On most installs the /var/lib/docker directory holds everything: image layers, container writable layers, volumes, and logs. If du shows volumes dwarfing everything else, your problem is data, not images, and pruning images will not fix it. If you want a deeper walkthrough of that directory layout, read where Docker stores data and how volumes differ from bind mounts before you start deleting things.

How to limit Docker container disk usage per container

The direct way to cap what a container writes is the storage-opt flag:

docker run -d --storage-opt size=10G my-app-image

In Compose it looks like this:

services:
  app:
    image: my-app-image
    storage_opt:
      size: 10G

That sets a 10 GB ceiling on the container’s writable layer. Think of it as a docker disk usage limit enforced by the storage driver: anything the app writes past that fails with a disk-full error inside the container, but the host disk stays safe. That is the tradeoff, and it is the right one: a single container failing beats every container failing.

There is one catch you need to know before relying on this. The size limit only works when the storage driver can enforce it. With the default overlay2 driver, it works on XFS filesystems with project quotas enabled, and on ZFS or Btrfs. On a plain ext4 root partition, which is what most budget VPS images ship with, Docker accepts the flag and silently ignores it. Check your setup with docker info | grep -i "storage driver|backing filesystem". If you are on ext4 and the limit matters to you, the honest options are to reformat the data partition as XFS with pquota or to move Docker’s data directory to a filesystem that supports quotas.

Volumes are not covered by storage-opt at all. A docker container storage limit only applies to the writable layer, so a volume can grow past any container limit because it lives outside that layer. That is why the next two sections matter more than the flag alone.

Cap container logs before they fill the disk

Logs are the quietest disk killer in Docker. By default, the json-file logging driver keeps everything a container prints, forever, in /var/lib/docker/containers/<id>/<id>-json.log. A chatty app behind a health check that logs every request can grow that file by gigabytes in a week, and Docker never rotates it on its own.

Set a global default in /etc/docker/daemon.json:

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

Then restart the daemon with sudo systemctl restart docker. Every container created after that keeps at most three 10 MB log files, or 30 MB per container. Existing containers keep their old settings, so recreate them to pick up the new limits. You can also set it per container with --log-opt max-size=10m --log-opt max-file=3 if one service needs a different budget. For a fuller treatment of log drivers and rotation, see the guide to Docker log management and stopping logs from filling the disk.

Prune images, containers, and build cache on a schedule

Even with limits in place, Docker accumulates dead weight: old image layers from every deploy, stopped containers nobody removed, and build cache from months ago. A weekly prune clears most of it:

docker system prune -a -f --volumes

Read the flags before you run it. -a removes all images not attached to a running container, not just dangling ones. --volumes removes anonymous volumes, which is where data loss happens if a container stored something important in an unnamed volume. Named volumes are safe. Run it by hand the first few times and check what it plans to delete before you automate it.

Once you trust it, put it in cron so it runs without you thinking about it. A scheduled docker prune disk space routine is the cheapest insurance on a small VPS:

0 3 * * 0 docker system prune -a -f --filter "until=168h" >> /var/log/docker-prune.log 2>&1

The until=168h filter only touches items older than a week, which leaves a recent rollback image in place. Pair this habit with a regular cleanup of old Docker images to free disk space on your VPS when you deploy often, since each deploy can leave a full image behind and a weekly prune is the only thing standing between you and a disk full of last month’s builds.

Move Docker’s data directory when the root disk is too small

Sometimes the real fix is not a limit but a bigger home. Many cheap VPS plans ship with a small root disk and a large attached volume, and Docker defaults to the small one. Move its data directory with the data-root setting in /etc/docker/daemon.json:

{
  "data-root": "/mnt/data/docker"
}

Stop Docker, move the existing directory with rsync -a /var/lib/docker/ /mnt/data/docker/, then start Docker again and confirm with docker info | grep "Docker Root Dir". Do not skip the rsync step or copy with a plain cp that drops permissions, or containers will fail to start with cryptic errors.

Set up disk space alerts so you get warned early

Limits and pruning buy you time, but a warning buys you the chance to act before the disk hits 100%. A simple cron check is enough for one server:

#!/bin/bash
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -ge 80 ]; then
  echo "Disk usage on $(hostname) is at ${USAGE}%" | mail -s "Disk warning" [email protected]
fi

Run it daily and set the threshold at 80%, not 95%. By 95% you are already one large log rotation away from trouble. A disk-space alert setup with email or Telegram notifications is a better fit if you run several servers, and it takes about ten minutes to wire up: see how to set up disk space alerts on your VPS before it runs out. Alerts are also worth pairing with broader monitoring, since a full disk often shows up first as a crashed database, as explained in the guide to monitoring VPS disk space before it fills up and crashes MySQL.

What to do when the disk is already full

If you are reading this with a full disk, do the fast wins first, in this order. First, prune the build cache and dangling images with docker system prune -a -f. Second, truncate the biggest container log files directly: truncate -s 0 /var/lib/docker/containers/*/​*-json.log frees the space instantly without restarting anything. Third, check for a volume that grew out of control with docker system df -v and decide whether its contents can be archived elsewhere.

Do not delete files inside /var/lib/docker by hand with rm unless you know exactly what a given directory holds. Docker’s internal layout is not meant to be edited directly, and deleting the wrong layer directory corrupts images in ways that only a full re-pull fixes. Use Docker’s own prune commands for its own data, and reserve rm for files you created yourself, like old backups or downloaded archives in your home directory.

Once the disk has breathing room, go back and put the limits from this guide in place. A cleanup without a limit is a countdown to the next outage.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.