sometimes you just want to clear logs of a docker container.
For instance, I’m running dnsmasq in a docker container and I’m troubleshooting raspberry PI PXE boot (yes to run ESXi-ARM stateless…)
And the dnsmasq is serving my homelab its domain.
Then it can be an annoyance when you run docker logs <container name> seeing all the log entries since the container started.
And you just want to start with a clean slate.
It is the way
Yes, there is a way. And when you google you’ll find more blog posts giving you the solution. Which is
pi@raspberrypi~ $ sudo docker inspect --format='{{.LogPath}}' <container name>
pi@raspberrypi~ $ sudo truncate -s 0 <path presented by previous command>
And it works great… but… typing 2 lines of code… copy / pasting the log path in the second command.. way too dificult 🙂
So what do you think abou this ?
pi@taspberrypi~$ sudo truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container name>)
Yes, it will erase all logging, but that is the purpose of this whole excercise.
Addendum
If the one-liner isn’t helping, how about creating a small script just for one purpose alone…. What if you could do something like
pi@raspberrypi~$ truncate-log dnsmasq
To truncate the docker logs of the dnsmasq docker container. How ? Well easy.
Just create a new file in /usr/local/bin
pi@raspberrypi~$ nano /usr/local/bin/truncate-log
and copy / paste this content into the file
#!/bin/sh
CONTAINER=$1
truncate -s 0 $(docker inspect --format='{{.LogPath}}' $CONTAINER)
After saving the file. (Ctrl-x), add the execution bit
pi@raspberrypi~$ chmod +x /usr/local/bin/truncate-log
And voila, the next time you need to truncate a docker container log, you just type truncate-log <docker container name>