0

The production environment is getting out of space and I just found out using this command that the logs are taking a ton of space:

> foo@bar:/var/lib/docker$ sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log" | grep total 

80G     total

My question now is how do I clear all of this logs? I'm not able to acess the /var/lib/docker/containers via cd. Also, is there any way to check how old these logs are? I would like to keep the logs from the last 30 days.

1 Answers1

2

You can delete old files with this oneliner:

find /var/lib/docker/containers/ -type f -mtime +30 \
    -name '*-json.log' -prune -exec rm -vf '{}' \;

The code searchs recursively in the directory /var/lib/docker/containers/ for files whose name matches '*-json.log' and their modification is greater than 30 days and then removes them.

ichramm
  • 6,437
  • 19
  • 30
  • I don't think this will actually release space while the containers are still running. The Docker daemon will continue to write to the files, and they'll keep consuming space, even though they don't have filesystem entries. – David Maze Oct 13 '21 at 15:05
  • I think that would unlikely considering these files haven't been modified in 30 days but yes, it may happen. I think this can be done only once (with the daemon stopped), and then use the linked answer to properly configure docker. – ichramm Oct 13 '21 at 15:33