0

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)?

CIsForCookies
  • 10,991
  • 7
  • 44
  • 96

1 Answers1

1

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
davidxxx
  • 115,998
  • 20
  • 192
  • 199
  • Would that be OK even if the file is constantly being written into? – CIsForCookies Apr 11 '21 at 15:03
  • 1
    Yes You would not have any error. I updated with a basic example. – davidxxx Apr 11 '21 at 17:32
  • There's always a risk you'll time the truncate in the middle of the file being written, at which point logs will be broken like this commenter saw: https://stackoverflow.com/questions/42510002/how-to-clear-the-logs-properly-for-a-docker-container/42510314?noredirect=1#comment98713207_43570083 – BMitch Apr 11 '21 at 20:06
  • @ BMitch That is right but probably because of json log processing performed by Docker. That should work even in corrupted json : `cat $(docker inspect -f='{{.LogPath}}' foo-container)` – davidxxx Apr 12 '21 at 08:41