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
The json-file logging driver does not rotate logs by default. In case you are looking on how to activate it instead of truncating them by hand, you can activate it by creating/updating /etc/docker/daemon.json
:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "15m",
"max-file": "5",
}
}
Note that you will need to restart docker (e.g. systemctl restart docker
) and the config only affects containers created after modifying the daemon configuration (e.g. recreate the container of a specific service in docker-compose: docker-compose up --force-recreate --no-deps -d <service-name>
).
For more information see: