Docker service generate multiple logs on system. The service generate its own log file and each container creates its own separate logs. If your system is getting out of disk space and you found that the docker container’s log files are consuming high disk space. you can find the log file location and clear them with the help of this tutorial. While clearing log files of a docker container, you don’t need to stop it.

This tutorial will help you clear the log file of a Docker container.

Advertisement

How to Clear Logs of a Docker Container Docker log

Clear Docker Container Logs

Below are the 3 different options to clear log files of Docker containers. Choose any one of the below options to truncate the docker container log files.

Clear Specific Container Logs

Before clearing the specific container logs, first you need to find the container ID or name. The following command will help you find it.

docker ps -a

Next, we will find the log file path and then truncate it. Use inspect option to find the log file name and location of a docker container.

docker inspect  --format='{{.LogPath}}' 

As a result, you will get a log file path. Now truncate the log file with the following command.

truncate -s 0 /path/to/logfile 

Here -s is used to set the size of a file. You provided 0 as input, which means completely truncating the log file.

You can combine both commands in a single command. Use the following command to truncate the log file of the specified Docker container.

truncate -s 0 $(docker inspect --format='{{.LogPath}}' ) 

Clear All Container Logs

You can also truncate the log files of all docker containers in your system.

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

Wrap Up

In this blog post, you have learned to truncate (clear) log files of a Docker container.