Docker: How to Clear Logs Properly for a Docker Container

Over time, Docker containers can accumulate log files that take up significant disk space—especially in long-running or verbose applications. If you’re running into storage issues or want a clean log state for debugging, clearing Docker container logs can help.

In this blog post, you’ll learn:

  • Where Docker stores logs
  • How to view and clear container logs
  • Safe practices to manage log sizes

🧾 Where Are Docker Logs Stored?

By default, Docker uses the json-file logging driver, and stores logs in:

/var/lib/docker/containers/<container-id>/<container-id>-json.log

This file contains all STDOUT and STDERR logs generated by the container.


👀 View Current Container Logs

To inspect logs from a running container, use:

docker logs <container-name>

For live logs:

docker logs -f <container-name>

But what if you want to clear them?


🧹 How to Clear Docker Logs (Without Restarting the Container)

You can truncate the log file using the following command:

sudo truncate -s 0 /var/lib/docker/containers/<container-id>/<container-id>-json.log

🔍 How to find the container ID:

docker inspect --format='{{.Id}}' <container-name>

🛑 Warning: Be sure you are targeting the correct container. Truncating the wrong file could lead to unexpected behavior.


🔁 Option: Restart Container to Reset Logs

If truncating feels risky, you can restart the container, which resets the logging file (if using log rotation):

docker container restart <container-name>

This is safer but causes downtime.


🔧 Pro Tip: Use Log Rotation to Avoid Large Logs

Edit or create /etc/docker/daemon.json:

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

Then restart the Docker daemon:

sudo systemctl restart docker

This keeps logs manageable automatically, by limiting file size and number of log files.


📝 Summary

TaskCommand or Action
View logsdocker logs <name>
Truncate log filetruncate -s 0 on *.json.log
Get container IDdocker inspect --format='{{.Id}}' <name>
Enable log rotationEdit daemon.json and set log-opts
Restart Dockersudo systemctl restart docker

✅ Conclusion

Cleaning Docker container logs is essential for long-term maintenance and disk space management. Whether you’re debugging or preparing for production, managing logs properly ensures better performance and clarity.

For best results, combine manual cleanup with log rotation settings, so you rarely have to worry about bloated logs again.

Sharing Is Caring:

Leave a Comment