Docker Log Rotation Setup for Small Servers: 2026 Guide

Master Docker log rotation setup for small servers in 2026. Prevent disk overflow and optimize performance. Read the guide now!

Futuristic server rack with glowing blue data streams, log rotation arrows, and Docker cubes.

If you run a small server with Docker, you may not think about logs. But Docker creates log files every day. These files grow fast. On a small VPS with limited disk space, this becomes a big problem. This guide shows you how to do a proper docker log rotation setup to keep your server running smoothly. We will cover three methods, from easy to advanced. By the end, your server will stay clean and healthy.

Why Docker Log Rotation Setup Is Critical for Small Servers

Small servers have small disks. A typical VPS may only have 10GB to 50GB of storage. Docker logs can eat up that space without warning. A docker log rotation setup stops this from happening. Let us look at why this matters so much.

The Hidden Disk Space Problem on Small VPS Instances

When you start a Docker container, it creates a log file. This file stores every message the container prints. For busy containers, this can be megabytes per hour. On a small VPS, you might only have a few gigabytes free. Without a docker log rotation setup, your logs can fill the disk in days or even hours. Many new server owners do not notice this until it is too late.

What Happens When Docker Logs Fill Your Disk

When your disk is full, bad things happen. Your containers may stop working. New containers cannot start. Your website goes offline. Database writes fail. In some cases, the whole server becomes unstable. A proper docker log rotation setup prevents all of these issues. It keeps log files small by deleting old data and keeping only recent entries.

How to Check Your Current Docker Log Size

Before you start, check how much space your Docker logs use right now. Run this command on your server:

sudo du -sh /var/lib/docker/containers/*/*-json.log

This shows the size of each container log file. You may be surprised by the results. Some logs can be several gigabytes. If you see large files, you need a docker log rotation setup right away. You can also check total log size with:

sudo du -sh /var/lib/docker/containers/

Docker Log Rotation Setup Method 1: Configure daemon.json (Recommended for Small Servers)

This is the easiest and best method for most small servers. You change one file, and Docker handles the rest. This docker log rotation setup applies to all containers on your server.

Step 1: Edit /etc/docker/daemon.json

Open the Docker config file with a text editor. Use nano or vim:

sudo nano /etc/docker/daemon.json

If the file is empty or does not exist, add the following JSON content. This is the core of your docker log rotation setup:

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

If the file already has content, add the “log-opts” section inside the existing JSON. Make sure the JSON format is correct. Missing commas or brackets will break Docker.

Step 2: Choose the Right max-size and max-file Values

The two key settings in this docker log rotation setup are max-size and max-file. Here is what they mean:

  • max-size: The maximum size of one log file before Docker starts a new one. Common values are “10m” (10 megabytes) or “50m” (50 megabytes).
  • max-file: The number of old log files to keep. If set to 3, Docker keeps the current log plus two rotated backups.

For a small server, “10m” and “3” is a good starting point. This means each container uses at most 40MB (10m × 4 files including the active one). You can adjust these values based on your disk space.

Step 3: Restart Docker and Verify the Setup

After saving the daemon.json file, restart Docker to apply changes:

sudo systemctl restart docker

Check that Docker is running correctly:

sudo systemctl status docker

To verify your docker log rotation setup is active, run:

docker info | grep -A 5 "Logging"

You should see “json-file” as the log driver with your max-size and max-file values. If you see these values, your setup is working.

Step 4: Apply Log Rotation to Existing Containers

Important: The daemon.json change only affects new containers. Existing containers keep their old settings. To apply this docker log rotation setup to existing containers, you must recreate them. Use these steps:

  1. Stop the old container: docker stop container_name
  2. Remove the old container: docker rm container_name
  3. Start a new container with the same settings

If you use Docker Compose, simply run docker compose up -d to recreate containers. The old log files will remain on disk until you delete them manually. You can clean them with:

sudo truncate -s 0 /var/lib/docker/containers/*/*-json.log

Docker Log Rotation Setup Method 2: Per-Container Configuration with Docker Compose

Sometimes you want different log settings for different containers. A database container may need more log space than a simple web app. This docker log rotation setup method gives you that control.

Adding Log Rotation Options to docker-compose.yml

Open your docker-compose.yml file. Add a logging section to each service. Here is an example:

services: web: image: nginx logging: driver: json-file options: max-size: "5m" max-file: "2"

This docker log rotation setup limits the web container to 5MB per file with 2 backups. Total space per container is 15MB. You can set different values for each service. After editing the file, run docker compose up -d to apply changes.

Using –log-opt Flags with docker run

If you do not use Docker Compose, you can add log options directly in the docker run command. Here is an example:

docker run -d --name myapp --log-opt max-size=10m --log-opt max-file=3 nginx

This docker log rotation setup applies only to the “myapp” container. Other containers use the default daemon.json settings. This method is useful when you run containers manually without Compose files.

When to Use Per-Container vs Daemon-Wide Settings

Use daemon-wide settings (Method 1) when you want a simple, consistent docker log rotation setup across all containers. This is best for most small servers. Use per-container settings (Method 2) when different containers have different needs. For example, a database may need larger logs for debugging, while a static web server needs very small logs. You can combine both methods: set defaults in daemon.json and override specific containers in docker-compose.yml.

Docker Log Rotation Setup Method 3: Host-Level Logrotate for Advanced Control

This method uses the Linux logrotate tool instead of Docker’s built-in feature. It gives you more control over compression and scheduling. This docker log rotation setup is best for advanced users who need fine-grained control.

Setting Up logrotate for Docker Container Logs

Create a new logrotate config file:

sudo nano /etc/logrotate.d/docker-containers

Add the following content:

/var/lib/docker/containers/*/*.log { rotate 3 daily copytruncate compress missingok notifempty }

Here is what each line does in this docker log rotation setup:

  • rotate 3: Keep 3 old log files
  • daily: Rotate logs every day
  • copytruncate: Copy the log then clear the original (important for Docker)
  • compress: Compress old logs to save space
  • missingok: Do not error if log file is missing
  • notifempty: Do not rotate empty log files

Compression and Retention Policies for Small Disks

On small servers, compression is very important. Compressed logs take much less space. The “compress” option in the config above uses gzip by default. You can also add “delaycompress” to only compress on the second rotation. For very small disks, consider keeping fewer rotations. Change “rotate 3” to “rotate 1” to keep only one old log. You can also add “size 10M” to rotate when the file reaches 10MB instead of daily.

Automating logrotate with Cron on Your Small Server

Logrotate usually runs via a daily cron job. Check if it is already set up:

cat /etc/cron.daily/logrotate

If this file exists, logrotate runs automatically every day. If not, create a cron job:

sudo crontab -e

Add this line to run logrotate daily at 2 AM:

0 2 * * * /usr/sbin/logrotate /etc/logrotate.d/docker-containers

This completes your host-level docker log rotation setup. Test it with: sudo logrotate -d /etc/logrotate.d/docker-containers

How to Choose the Right max-size and max-file for Your Small Server

Picking the right values is key to a good docker log rotation setup. Too small, and you lose useful log data. Too large, and your disk fills up. Let us look at how to find the right balance.

Sizing Formula Based on Disk Capacity

Use this simple formula to find good values for your docker log rotation setup:

Total log space = max-size × (max-file + 1) × number of containers

As a rule, Docker logs should not use more than 10-20% of your total disk. For a 20GB disk, that is 2-4GB for all logs. If you run 5 containers, each container gets 400-800MB. With max-size=10m and max-file=3, each container uses 40MB. That is very safe.

Recommended Settings for Common VPS Sizes (10GB, 20GB, 50GB)

VPS Disk Size Number of Containers Recommended max-size Recommended max-file Total Log Space
10GB 1-3 5m 2 45-90MB
20GB 3-10 10m 3 120-400MB
50GB 5-20 20m 5 600MB-2.4GB

These values work well for most small server docker log rotation setup needs. Adjust based on your actual log output.

Balancing Log Retention vs Disk Space

Think about what you need from logs. If you only check logs when something breaks, you need less retention. Set max-file to 2 or 3. If you need to debug issues that happened days ago, keep more files. But remember: on a small server, disk space is precious. A good docker log rotation setup keeps logs just long enough to be useful, then removes them. You can always increase the values later if you find you need more history.

Troubleshooting Common Docker Log Rotation Issues on Small Servers

Sometimes things do not work as expected. Here are common problems with docker log rotation setup and how to fix them.

Settings Changed But Logs Still Growing

If you changed daemon.json but logs keep growing, check these things:

  • Did you restart Docker? Run sudo systemctl restart docker
  • Are you checking old containers? The new settings only apply to new containers. You must recreate old ones.
  • Is the JSON format correct? A missing comma breaks the whole file. Test with docker info to see if settings loaded.
  • Did you edit the right file? The path must be /etc/docker/daemon.json exactly.

Permission Errors in /var/lib/docker

If you see permission errors when Docker tries to rotate logs, check file ownership:

ls -la /var/lib/docker/containers/

The files should be owned by root. Fix permissions with:

sudo chown -R root:root /var/lib/docker/containers/

Also check that the daemon.json file has correct permissions:

sudo chmod 644 /etc/docker/daemon.json

After fixing permissions, restart Docker and verify your docker log rotation setup again.

Verifying Log Rotation Is Actually Working

To confirm your docker log rotation setup works, follow these steps:

  1. Generate some log activity: docker logs container_name
  2. Check the log file size: sudo ls -lh /var/lib/docker/containers/*/*-json.log
  3. Wait for the log to reach max-size, then check again. You should see rotated files like *-json.log.1, *-json.log.2
  4. Check that old files are being removed when max-file is exceeded

If you see rotated files appearing and old ones being deleted, your docker log rotation setup is working correctly.

FAQ: Docker Log Rotation Setup for Small Servers

Q: Does docker log rotation setup delete logs immediately?

A: No. Docker keeps the number of files you set in max-file. When a new rotation happens, the oldest file is deleted. You always have recent logs available.

Q: Can I use docker log rotation setup with the syslog driver?

A: The max-size and max-file options only work with the json-file and local log drivers. If you use syslog, journald, or other drivers, log rotation is handled by the system, not Docker.

Q: Will docker log rotation setup affect running containers?

A: Changing daemon.json does not affect running containers. You must recreate containers to apply new settings. Plan this during a maintenance window.

Q: What if I need to keep logs for a long time?

A: For long-term log storage, send logs to an external service. Use the syslog or gelf driver to forward logs to a log server. Keep the local docker log rotation setup small for recent debugging only.

Q: How do I clean up old log files that are already too large?

A: Use the truncate command to empty them without deleting: sudo truncate -s 0 /var/lib/docker/containers/*/*-json.log. Then recreate containers to apply your new docker log rotation setup.

Conclusion and Final Recommendations

A proper docker log rotation setup is not optional for small servers. It protects your disk space, keeps your containers running, and prevents unexpected downtime. Here are the key takeaways from this guide:

  • Always set up log rotation when you install Docker on a small VPS
  • Method 1 (daemon.json) is the easiest and best for most users
  • Use per-container settings only when you have specific needs
  • Method 3 (logrotate) adds compression but is more complex
  • Start with conservative values like 10m max-size and 3 max-file
  • Always recreate containers after changing daemon.json
  • Check your log sizes regularly to catch problems early

Your next steps should be:

  1. Check your current Docker log sizes today
  2. Choose a docker log rotation setup method that fits your needs
  3. Apply the settings and recreate your containers
  4. Set a reminder to check log sizes once a month

With these steps done, your small server will run smoothly. You will never worry about Docker logs filling your disk again. A good docker log rotation setup is one of the simplest and most important things you can do for server health.