Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All combined docker logs with container name

So I am trying to get the combined output of all the container logs with its container name in a log file docker logs --tail -3 <id> with **container name** >> logst.log file.

like image 399
Akhil Gunuganti Avatar asked Dec 03 '25 10:12

Akhil Gunuganti


1 Answers

docker logs takes a single command so you would have to run them for each container. I guess I could do something along:

docker ps --format='{{.Names}}' | xargs -P0 -d '\n' -n1 sh -c 'docker logs "$1" | sed "s/^/$1: /"' _
  • docker ps --format='{{.Names}}' - print container names
  • xargs - for input
    • -d '\n' - for each line
    • -P0 - execute in parallel with any count of parallel jobs
      • remove this option if you don't intent to do docker logs --follow
      • it may cause problems, consider adding stdbuf -oL and sed -u to unbuffer the streams
    • -n1 - pass one argument to the underyling process
    • sh -c 'script' _ - execute script for each line with line passed as first positional argument
      • docker logs "$1" - get the logs
      • sed 's/^/$1: /' - prepend the name of the docker name to each log line

But a way better and industrial grade solution would be to forward docker logs to journalctl or other logging solution and use that utility to aggregate and filter logs.

like image 111
KamilCuk Avatar answered Dec 04 '25 23:12

KamilCuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!