Blog

Why and How: Docker Log rotation

Logging is important for development as well as for production systems. Logging help us troubleshoot better.
Docker provides us Container logs in JSON format by default

which are stored in following file (linux)

/var/lib/docker/containers/$containerid/$containerid.json

Now, what if
too much of logging gives you storage full issue
Or
You want to keep new logs for containers and discard old ones.

By default, Docker keeps log of a container from the time it’s running and of no size limit.

For better log management, it is recommended that you manage log files for days for troubleshooting and it doesn’t eat all space of your server, resulting in downtime.

Let’s go through each step and configure docker log management for our system

Step 1:

Go to /etc/docker folder

Step 2:

Create a file named as daemon.json`
– add the following content

{
   "log-driver": "json-file",
   "log-opts": {
       "max-size": "10m",
       "max-file": "3"
   }
}
  • It defines, logging driver as JSON, which is default
  • max-size, is the max file size for container logs, when this size exceeds, docker will rotate logging file and create a new one for newer logs.
  • max-file, is a maximum number of logging files that docker will store, after this limit, Docker will remove the old file and create a space for new logs.

As per the above example, a maximum of 30 MB of logs will be saved by any docker container. After that, your old logs will be rotated/deleted to make space for newer logs.
– You can take a backup before it rotates if you want to use it later

Step 3:

After saving this file, you need to restart docker, to make changes reflect in containers that will be created after this change

 service docker restart 

Remember: it will not reflect in currently running docker container, will be applicable to new coming containers.

We can provide these 2 limits in the docker run command as well,

 --log-opt max-size=10m --log-opt max-file=5 

append above in your docker run command to make it custom for that particular container

Do Comment and Share if you find this content useful.
Read more on Docker and AWS here

FAQ

1. How do I limit the size of a docker log?

You can limit docker log storage at the server level as well as in particular docker run command
To do so at the server/instance level, you need to edit daemon.json file of Docker configuration and restart the Docker daemon.
To do this at the container level, you can append the following configuration in run command
--log-opt max-size=10m --log-opt max-file=5

Drafted On,
February 15, 2020
DevOps
identicalCloud.com

References:
– https://docs.docker.com/config/containers/logging/json-file/

Summary
Why and How: Docker Log rotation
Article Name
Why and How: Docker Log rotation
Description
Why Docker log rotation is needed and how to do docker log rotation. If you are getting disk full issue after docker installation, this article will help you eliminate the issue
Author
Publisher Name
IdenticalCloud

Leave a Comment