If you’re using the default json-file
driver, set max-file
and max-size
appropriately to have Docker rotate and remove old logs automatically.
Here’s how to reduce the size of your docker log drastically. Perfect for testing or a home lab where you run long lived stand alone containers and don’t need to keep a long audit trail of logging.
Check size
docker inspect --format='' $(docker ps -a -q) | sudo xargs -n 1 du -ah
Docker compose
A maximum of five log files with a max file size of 10 Mb each is set for example. As a result, the container can store up to 50 Mb worth of data. Adjust those parameters as needed.
Remember to put " "
around the values:
my-app:
image: my-app:latest
logging:
driver: "json-file"
options:
max-file: "5" # number of files or file count
max-size: "10m" # file size
Configure the default logging driver
To make the Docker daemon default to a certain logging driver, set the name of
that driver as the value for log-driver
in the daemon.json
configuration
file.
You can also configure this as a default in your daemon setting. In fact, we can
even use the local
driver as the default logging driver, The local logging
driver captures output from the container’s stdout/stderr and writes them to
internal storage that is optimized for performance and disk use. set the
log-driver
and log-opt
keys to appropriate values in the daemon.json
file:
The local driver by default keeps 100MB of log messages per container and uses automatic compression to reduce the size of the disk. The 100MB default value is calculated on the basis of a 20M file size and a default count of 5 such files (to take into account log rotation). We can shrink it even further
If the daemon.json
does not exist we can create it for the first time and
then restart docker
sudo cat <<EOF > /etc/docker/daemon.json
{
"log-driver": "local",
"log-opts": {
"max-size": "10m"
}
}
EOF
systemctl restart docker
Logs are stored in a custom format designed for minimal overhead.