View and rotate Docker container logs

Read, follow and filter Docker container logs, then set log rotation with max-size and max-file so container logs never fill your VPS disk again.

  • Beginner
  • Linux VPS
  • 4 min read

By RunRacks Team · Published

Docker captures everything a container prints to standard output and standard error. That makes debugging easy, but by default Docker keeps those logs forever, and a chatty container can quietly fill the disk. This guide shows how to read logs efficiently and how to cap their size for every container on the server.

Before you start

  • A Linux VPS with Docker Engine installed. See Install Docker and Docker Compose.
  • A user that can run docker commands, and sudo for editing /etc/docker/daemon.json.
  • A few minutes where restarting containers is acceptable, because the rotation settings apply when containers are created.

Read logs with docker logs

Show everything a container has logged:

Terminal
docker logs web

The options you will use most:

  • -f follows new lines as they arrive, like tail -f. Press Ctrl + C to stop.
  • --tail 100 shows only the last 100 lines.
  • --since 30m or --since 2026-11-09T10:00:00 limits output by time.
  • -t adds a timestamp to every line.

Combine them:

Terminal
docker logs -f --tail 50 -t web

docker logs writes the container's standard error to your terminal's standard error. To search everything with grep, merge the two streams:

Terminal
docker logs --since 1h web 2>&1 | grep -i error

Read logs with Docker Compose

From the folder with your compose.yaml:

Terminal
docker compose logs -f --tail 50
docker compose logs -f app

The first follows every service in the project with a coloured prefix per service. The second follows only app.

Where the logs are stored

With the default json-file driver, each container's log is a file on the host:

Terminal
docker inspect --format '{{.LogPath}}' web

It is usually under /var/lib/docker/containers/<id>/. Check how big these files are:

Terminal
sudo du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail

If one file is several gigabytes, you have found your disk problem.

Set log rotation for all new containers

Edit the daemon configuration:

Terminal
sudo nano /etc/docker/daemon.json

If the file is empty or missing, add:

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

Each container now keeps at most three files of 10 MB. If the file already has other settings, add the two keys inside the existing braces and keep the JSON valid.

Docker also offers a local log driver, which compresses rotated files and rotates by default. It is a good choice too, but json-file with limits is the most widely supported by log tools.

Restart Docker to load the change:

Terminal
sudo systemctl restart docker

If Docker refuses to start, the JSON is invalid. Check it with:

Terminal
sudo dockerd --validate --config-file /etc/docker/daemon.json

Apply rotation to existing containers

The new defaults apply only to containers created after the change. Existing containers keep their old settings until you recreate them. With Compose:

Terminal
docker compose up -d --force-recreate

You can also set limits per service in the Compose file, which overrides the daemon defaults:

YAML
services:
  app:
    image: myapp:1.0
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

For a single docker run container, add --log-opt max-size=10m --log-opt max-file=3.

Free space from a huge log right now

If the disk is already full and you cannot recreate the container yet, empty its log file in place:

Terminal
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' web)"

This deletes the old log lines but keeps the container running. Do not delete the file with rm, because Docker keeps it open and the space would not be freed.

Check it works

Confirm a container has the new limits:

Terminal
docker inspect --format '{{.HostConfig.LogConfig}}' web

You should see {json-file map[max-file:3 max-size:10m]}. Over the following days, the log folder for that container should never hold more than three files.

Troubleshooting

  • Docker fails to start after editing daemon.json: a missing comma or quote. Run the validate command above, fix the file, then start Docker again.
  • Rotation does not apply: the container was created before the change. Recreate it.
  • `docker logs` shows nothing: the app logs to a file inside the container, not to standard output. Check the image docs for a setting that logs to the console.
  • `docker logs` says the driver does not support reading: some drivers, such as syslog, send logs elsewhere. Read them in the destination system, or switch back to json-file or local.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.