Logs are essential for monitoring and debugging containers, but without proper rotation, they can grow uncontrollably and take up unnecessary disk space. If you use Docker, configuring log rotation is crucial to maintaining an efficient environment.
In this article, you will learn how to configure log rotation effectively in both Docker.
🚀 Why Is Log Rotation Important?
Without control, log files can grow indefinitely, impacting performance and even crashing the system due to a lack of disk space. Log rotation allows you to define a maximum log file size and the number of stored log files before automatic deletion.
🔧 Configuring Log Rotation in Docker
In Docker, logs are stored in JSON format by default. To configure log rotation, follow these steps:
1️⃣ Edit the Docker Configuration File
Open the /etc/docker/daemon.json
file:
sudo mkdir -p /etc/docker/ && sudo touch /etc/docker/daemon.json
sudo vim /etc/docker/daemon.json
Add the following lines (if the file already contains other configurations, adjust the JSON accordingly):
{
"log-driver": "json-file",
"log-opts": {
"max-size": "500m",
"max-file": "3"
}
}
Explanation of Parameters:
"log-driver": "json-file"
→ Defines JSON as the log storage format."max-size": "500m"
→ Limits the maximum size of each log file to 500MB."max-file": "3"
→ Keeps a maximum of 3 log files per container.
2️⃣ Restart Docker
sudo systemctl restart docker
Now, container logs will be limited to 500MB per file, with a maximum of 3 files per container.
🔍 How to Verify the Configuration?
After applying the configuration, you can check if everything is correctly set up.
In Docker:
Check if the log driver is correctly configured:
docker info | grep "Logging Driver"
List container log files:
ls -lh /var/lib/docker/containers/*/*.log
💡 Who Am I?
I am Gabriel Carmo, passionate about technology (especially Open Source). I have experience in Cloud, Kubernetes, OpenShift, Zabbix, Dynatrace, and more! Always exploring new technologies and sharing knowledge. 🚀
📬 Let's Connect?
🔗 LinkedIn
🐙 GitHub
🦊 GitLab
🏅 Credly
📧 Contact: contato@gabrielandre.com.br
