The default setting for the log driver is a JSON file format, and the only way I can think of to reliably parse that involves parsing the file from the beginning, which I suspect is exactly what docker does. So the I'm not sure there's an option to do exactly what you are asking. However, there are two log options you can adjust when starting a container with the default JSON log driver.
- max-size: this limits how large a single JSON log file will grow to. After this, docker will create a new file. By default it is unlimited (-1).
- max-file: this limits the number of JSON log files that will be created up to the max size set above. By default it set to 1.
You can read about these options here: https://docs.docker.com/config/containers/logging/json-file/
I typically set these options with new default values for all containers being run on the docker host using the following lines inside my /etc/docker/daemon.json file:
{
"log-driver": "json-file",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
Those two options say to keep up to 3 different 10 meg JSON log files. The result is a limit between 20-30 megs of logs per container. You need to trigger a reload on the dockerd process to load this file (killall -HUP dockerd
or systemctl reload docker
).
You can override this on an individual container by passing the log options on your run command (or inside the compose file):
docker container run --log-opt max-size=5m --log-opt max-file=2 ...
There does not appear to be a way to change the logging options of an existing container, so you will need to recreate your containers to apply these changes.
The end result is that docker may still have to parse the entire file to show you the most recent logs, but the file will be much smaller with automatically rotating logs than the default unlimited logging option.