by Ying Kit Yuen
How to setup log rotation for a Docker container
We all need logs!
Sometimes working with Docker makes me feel like I’m working with a black box. Especially when I’m playing with Docker images from the community, and it doesn’t go the way I expected. In many cases, reading logs takes up a large portion of time when debugging.
This article is about setting up log rotation for Docker containers.
The default logging driver
We can configure different logging drivers for containers. By default, the stdout and stderr of the container are written in a JSON file located in /var/lib/docker/containers/[container-id]/[container-id]-json.log. If you leave it unattended, it can take up a large amount of disk space, as shown below.
Purge the log manually
If this JSON log file takes up a significant amount of the disk, we can purge it using the following command.
We could setup a cronjob to purge these JSON log files regularly. But for the long term, it would be better to setup log rotation.
Setup the log rotation
Configure the default logging driver
This can be done by adding the following values in /etc/docker/daemon.json. Create this file if it doesn’t exist.
The json-file logging driver has a few more options, and we can even change to other logging drivers such as syslog. For more information, please refer to the Docker Docs — Configure logging drivers.
Execute the following commands to reload the updated daemon.json. The new configuration will apply to all newly created containers after restart.
Configure the logging driver for a container
The configuration can also be done on the container level if you do not want to apply it globally.
The docker run command
We can specify the logging driver and options in the docker run command. For example:
The logging driver and options can also be configured using docker-compose. For example:
Verify if the setup is working.
Although the default settings work fine, you never know when the container logs take up all the disk space. This can be avoided by the few steps discussed above. Other than that, logs are an important asset. They are not only useful when something goes wrong, but they also contain a lot of hidden value. So never let the logs go.
— Originally posted on Boatswain Blog.