how to truncate all logs from docker containers on a node?

there are situations when you need to quickly free up space on your kubernetes worker nodes. sometimes just temporarly to extend the vg for the local filesystem.

here is a quick way to truncate the logs of the docker containers on a node

first you have to get the docker containers on the host

we just need the container IDs so we use the option “-q”:

docker ps -q

then we want to get the logpath of every container:

docker inspect –format='{{.LogPath}}’ <container name>

we use the magic of bash to show us the logs of every container:

docker inspect –format='{{.LogPath}}’ $(docker ps -q)

and finally we can truncate them to free up some space in the filesystem:

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

we can verify the disk usage of the logs with the following command:

du -h $(docker inspect –format='{{.LogPath}}’ $(docker ps -q))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s