Tech Tip: docker logs
This tech tip regards logging file handling and its troubleshooting.
About docker logging
Every container (linux based), places its stdout to a log file located in /var/lib/docker/container/<container_inspect_id>/<file>-json.log .
Every time you call the function docker logs <container_id> you I’ll see the content of its file. This function with –f option acts like tail, it shows all file content and waits for new log contents (follow mode). This could be useful for troubleshooting; but during the time, in some “verbose” container, the log file could become huge. In fact after some months, docker logs will show you the whole content from the beginning of the container creation.
Where to find a specific container log?
1 |
docker inspect container_id | grep "LogPath" |
File reduction becomes critical in some scenario. The solutions to reduce this file are simply 2 (until now):
- log rotation
- truncate file
Log rotation
Simply place a file a “docker-container” file in /etc/logrotate.d/ with this content:
1 2 3 4 5 6 7 8 9 |
/var/lib/docker/containers/*/*.log { rotate 7 daily compress size=1M missingok delaycompress copytruncate } |
Source: https://sandro-keil.de/blog/2015/03/11/logrotate-for-docker-container/
Truncate
For single container:
1 |
truncate -s 0 /var/lib/docker/containers/container_id/file-json.log |
For multiple logs
1 |
truncate -s 0 /var/lib/docker/containers/*/*-json.log |