1

On my host where docker installed a file names -json.log which in this tree

Docker
  |_ builder
  |_ buildkit
  |_ containerd
  |_ containers
       |_ container-id
             |_ **container-id-json.log**

which consumes lot of space which goes on increasing , I want to know what is the purpose of this file ? and how to clean up this file to save space , if clean up what will be the consequences .

2 Answers 2

3

The purpose of these files is to keep the container's execution logs and will be useful to preserving memory and preserve the log history if the server is restarted.

You can clean these files, but there will be consequences:

One is the loss of the history record of the container associated with the clean file. Another is a docker log writer error if the file is open at cleanup time, depending on the method used for cleanup.

There are many answers associated with the question: "how to clear docker logs?"

So I'm going to summarize and point to them.

You can try the truncate command to clean the file:

sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"

Another way is to delete and recreate your containers, this will reset your logs.

However, the best way, in my opinion, is to rotate the logs through the docker. As described in the official documentation about logging.

docker run --log-opt max-size=10m --log-opt max-file=5 my-image:<version>

Credits and related answers:

Official docker documentation for logging in compose file: https://docs.docker.com/compose/compose-file/compose-file-v3/#logging

0
#!/bin/sh

echo "======== start clean docker containers logs ========"

logs=$(find /var/lib/docker/containers/ -name *-json.log)



for log in $logs

do

echo "clean logs : $log"

cat /dev/null > $log

done

echo "======== end clean docker containers logs ========"
chmod +x clean_docker_log.sh

./clean_docker_log.sh

2.Set Docker container log size

This is achieved by configuring the max-size option of the container docker-compose

1
  • what does this script do, i only see echo command Sep 23, 2021 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.