Docker Log Management: Stopping Logs from Filling Your Disk

Docker Log Management: Stopping Logs from Filling Your Disk

Docker log management is the habit of limiting, rotating, and cleaning container logs before they eat your disk. By default, Docker keeps every line a container writes to stdout and stderr, and on a busy server that file can grow for months without warning. One chatty app, one debug loop left on, or one error repeated every second is enough to fill a small VPS disk and take down every container on it.

This guide shows a setup that works on a single VPS: find the logs that are growing, set size limits for new containers, rotate the logs you already have, and add a cleanup routine you can leave running. You need SSH access and a user that can run Docker commands. The examples use the default json-file log driver, which is what most single-server installs use.

Why Docker Logs Grow Without Warning

Docker does not treat container output as temporary data. When an app writes to stdout, Docker appends that line to a JSON log file on the host, usually under /var/lib/docker/containers/. The file keeps growing until you limit it, rotate it, or delete the container. Restarting the container does not shrink it. Restarting Docker does not shrink it either.

The risk is worse on a VPS because disk space is fixed and shared. When the disk hits 100 percent, containers fail to write, databases stop, and Docker itself can refuse to start new containers. You often find out from a monitoring alert, or from a site that stops loading, rather than from Docker. That is why checking disk use belongs in the same routine as checking container status. If you already track host resources, fold log size into that check: our guide to checking VPS resource usage with htop, df, and free shows the commands that surface a full disk before it becomes an outage.

Find Which Containers Are Using the Disk

Start with the whole picture, then narrow it down. Run:

docker system df
df -h
du -sh /var/lib/docker/containers/*/*-json.log 2>/dev/null | sort -rh | head -20

The first command shows how much space Docker images, containers, volumes, and build cache use. The second shows host disk use. The third lists the largest container log files, biggest first. If the third command returns nothing, your install may store logs elsewhere, or your shell may not expand the path; check one container directly instead:

docker inspect --format='{{.LogPath}}' container-name
ls -lh $(docker inspect --format='{{.LogPath}}' container-name)

Match the biggest files to containers with docker ps, then look at what the app is actually logging:

docker logs --tail 100 container-name

If the last 100 lines are the same error repeated, fixing the app matters more than deleting the log. Deleting buys you days. Fixing the error buys you months. Check the app config for a debug flag, a verbose flag, or a retry interval you can raise before you move on to limits.

Set Log Limits for New Containers

Docker log rotation is controlled by the logging driver options max-size and max-file. The setting max-size caps each log file, and max-file caps how many rotated files Docker keeps. Once the limit is hit, Docker rotates to a new file and drops the oldest one. Set both, or rotation will not behave the way you expect.

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

With those values, each container keeps at most three 10 MB files, or about 30 MB of logs in total. That is plenty for troubleshooting a recent problem and small enough that logs cannot fill a disk on their own. Adjust the numbers to your disk: 10m and 3 files suit a small VPS, while a busier app you actively debug might need 50m and 5 files. After editing the file, restart Docker:

sudo systemctl restart docker

Note that daemon.json only affects containers created after the restart. Existing containers keep their old settings, which is a common source of confusion: you set the limit, restart Docker, and the old log keeps growing. For existing containers, set the limit per container at recreate time instead.

In a Compose file, the same options go under logging:

services:
  web:
    image: nginx
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

If you are still writing your first stack, our walkthrough of Docker Compose and your first file covers where the logging block sits alongside ports, volumes, and environment variables. After you change a Compose file, recreate the container with docker compose up -d so the new logging options take effect. A plain restart will not apply them.

Fix Log Limits on Containers Already Running

You cannot change logging options on a running container. You have to recreate it. That sounds disruptive, but for a stateless container it is a normal operation: stop it, remove it, and start it again with the same volumes and environment. Data in named volumes survives. Data written inside the container filesystem does not, so check that first.

With Compose, the safe sequence is:

docker compose down
docker compose up -d

With plain docker run, copy the original run command or inspect the container for its mounts and environment first, then remove and recreate it. If you are unsure what a container holds, back it up before you recreate it. Our guide to backing up Docker volumes automatically gives you a repeatable method that works before any recreate, move, or rebuild.

One pitfall to avoid: truncating a live log file with echo or rm while the container runs. Deleting the file does not free the space while Docker still holds the file open, and the disk use can look unchanged until the container restarts. Recreate the container, or stop it, truncate the file, and start it again. Do not delete files directly from /var/lib/docker unless you know exactly which container owns them.

Docker Logs Cleanup and Disk Space Safety

For a one-time cleanup, docker system prune removes stopped containers, unused networks, and dangling images. It does not delete logs of running containers and does not delete named volumes unless you add the volume flag, so read the prompt before you confirm:

docker system prune
docker system df

Run docker system df again after the prune to confirm the docker disk space came back. If you need more room, check build cache and unused images separately, and remove only images you can rebuild. Never run prune with the all and volumes flags on a production host without a backup, because that combination removes data you cannot rebuild.

Reduce Log Volume at the Source

Limits protect your disk, but they also throw away old lines. If you need logs for debugging, reduce volume at the source instead of only capping it. Turn off debug logging in production, and keep it for a staging container only. Send access logs from a reverse proxy to a separate file with its own rotation, rather than letting every health check hit stdout. Raise retry intervals on workers so a failing job logs once a minute instead of once a second.

For apps you must keep verbose, consider a different driver. The syslog or journald driver sends logs to the host logging system, where logrotate and journald already handle size limits and retention. That adds setup, so keep json-file for most single-server stacks and switch only when you have a clear reason, such as central log collection or a compliance rule about retention. The json-file driver with max-size and max-file covers the common case well.

When you move a stack to a new host, bring the logging settings with it. A fresh install with default settings will start growing logs from day one, and the problem will return on the new server. Our guide to moving Docker containers to a new VPS covers the move itself; add the daemon.json or Compose logging block to your migration checklist so the new host starts with limits in place.

Docker Log Management Checklist

Work through this list once, then repeat the check monthly:

  • Run docker system df and df -h, and list the largest container log files.
  • Read the tail of the biggest logs and fix repeated errors or debug logging at the source.
  • Set max-size and max-file in daemon.json or in each Compose file.
  • Recreate existing containers so the new limits apply; a restart alone is not enough.
  • Confirm the limits with docker inspect, and confirm disk use after a prune.
  • Repeat the disk check monthly, or after any deploy that adds a new container.

Logs should help you fix problems, not cause the next one. A 30 MB cap per container, a monthly disk check, and one fix for the noisiest app will keep most single-server stacks out of trouble.

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.