Why rotating Docker logs is important — How to rotate Docker logs
Are your docker containers taking all disk space? Are you facing issues like disk space is full while the only thing running on sever is docker container(s)
In this case, Docker log could be possibly taking all your disk space and you need to remove that. Manually removing logs can be a hectic job, so it is necessary that docker logs are rotated at certain intervals, In this blog, we will see how to configure the same.
You first need to verify the exact reason for the disk usage issue, Run the below-mentioned commands to check if Docker is the one who is taking all disk space
- SSH in your Linux machine
- Go to
/var/lib/docker
directory (Ubuntu) - Run below-mentioned command
du -sch *
- The output will look something like this
Here, if the total size is nearly equal to your disk space, Docker could be the reason for the problem. In this case, We need to set up the docker log rotation policy
Also, check if the containers
directory is taking most space among all docker files and directories
Now, Let’s set up rules so docker can automatically rotate files when it exceeds a certain amount of file size and we will also customize how many such files can be generated
- Go to
/etc/docker
directory - Edit/Create
daemon.json
file and add the below-mentioned code
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Code Suggest that; Maximum 3 files, 10MB each can be created per container, after which old logs will be rotated (deleted)
Save this file and restart docker
service docker restart
Changes are now effective, but only for new containers, for old containers settinngs are not upgraded (you need to re-run old containers, to make it effective)
All your new containers will inherit these rules by default.
These are global settings for all docker containers on this machine, what if you want to override it for some docker containers for which may be a different log retention policy is required?
For that, you can add the below-mentioned configuration in the docker run
command,
docker run --name mysql --restart always --log-opt max-size=10m --log-opt max-file=5 -v /my/own/datadir:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-password -d mysql:5.6
using this way, this particular docker container will take max-size=10MB and max-file=5
Hope this helps you!!!
Connect with me for more such DevOps related blogs
Drafted on: 22nd December 2020
Written by,
Ishita Shah
https://identicalcloud.com/