I have a very large log file under /var/lib/docker/container/<container_hash>/...-json.log
Is it possible to remove it while the container is still running? Would it create a new one instead and keep writing into that (preferred option)?
I have a very large log file under /var/lib/docker/container/<container_hash>/...-json.log
Is it possible to remove it while the container is still running? Would it create a new one instead and keep writing into that (preferred option)?
No : Docker is not designed to re-create the log file if you delete it manually.
A better approach if logs consistency doesn't matter for you would be to clear the content of the log file.
You can do it with (sudo is required because the owner/group of docker stuffs is root):
sudo sh -c "truncate -s 0 /var/lib/docker/container/<container_hash>/...-json.log"
About your question :
Would that be OK even if the file is constantly being written into? –
No error at least. Here a simple Linux example to check that :
Run a container that writes logs every 0.5 second :
docker run -d --name while-true alpine sh -c "while true; do date; sleep 0.5s; done"
Show 20 last logs :
docker logs --tail=10 while-true
Sun Apr 11 17:29:27 UTC 2021
Sun Apr 11 17:29:27 UTC 2021
Sun Apr 11 17:29:28 UTC 2021
Sun Apr 11 17:29:28 UTC 2021
Sun Apr 11 17:29:29 UTC 2021
Sun Apr 11 17:29:29 UTC 2021
Sun Apr 11 17:29:30 UTC 2021
Sun Apr 11 17:29:30 UTC 2021
Sun Apr 11 17:29:31 UTC 2021
Sun Apr 11 17:29:31 UTC 2021
Truncate the log to 0 byte 10 times :
for i in $(seq 1 10); do truncate -s 0 $(docker inspect -f='{{.LogPath}}' while-true); done
Show 20 last logs :
docker logs --tail=10 while-true
Sun Apr 11 17:29:35 UTC 2021
Sun Apr 11 17:29:35 UTC 2021
Sun Apr 11 17:29:36 UTC 2021