This tutorial will help you to clear a Docker container’s log file to free disk space and improving system performance. It is important to regularly managing log files, especially when running multiple containers that generate large amounts of data over time. By following these steps, you can easily reduce the size of your logs without disrupting the operation of your containers.
Clear Docker Container Logs
Here are 2 different ways to clear Docker container logs. You can pick any one of these methods to clear the logs.
1. Clear Specific Container Logs
Before you clear a specific container’s logs, you need to find its container ID or name. Run the command below to find it:
docker ps -a
Next, find the path of the log file by using the following command to inspect the container:
docker inspect --format='{{.LogPath}}' <container_name_or_id>
This command will give you the path of the log file. Once you have the path, you can clear the log file with the following command:
truncate -s 0 /path/to/logfile
The -s 0
option sets the file size to 0, which completely clears the log file.
You can also combine the two commands into one to directly clear the log file of a Docker container. Use the following command for this:
truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
2. Clear All Container Logs
You may also need to clear the log files of all Docker containers on your system. Run the following command to clear all container logs:
truncate -s 0 /var/lib/docker/containers/*/*-json.log
In some cases the default docker containers directory (/var/lib/docker/containers/
) may vary based on installation type.
Wrap Up
In this tutorial, you have learned about how to clear the log files of a Docker container, both for individual containers and all containers at once. Managing log files is a simple but effective way to prevent your system from running out of disk space. Make it a habit to check and clear unnecessary logs regularly, that will ensure your Docker environment runs smoothly and efficiently.
4 Comments
Thank you!! Great explanation solved my docker utilisation.
truncate -s 0 /var/lib/docker/containers/*/*-json.log
When trying to run this cmd, I get
truncate: cannot open ‘/var/lib/docker/containers/*/*-json.log’ for writing: No such file or directory.
I can see the file and filepath but cant understand why im getting this error?
Any suggestions?
Thank you for this article. My /var/lib/docker was 95% and Zabbix was sending me an email every few minutes and now I have resolved the issue.
You are a breath of fresh air and can find easy solutions without all the technobabble.
oneline:
docker inspect –format='{{.LogPath}}’ | xargs truncate -s 0