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.
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 namesxargs - for input
-d '\n' - for each line-P0 - execute in parallel with any count of parallel jobs
docker logs --followstdbuf -oL and sed -u to unbuffer the streams-n1 - pass one argument to the underyling processsh -c 'script' _ - execute script for each line with line passed as first positional argument
docker logs "$1" - get the logssed 's/^/$1: /' - prepend the name of the docker name to each log lineBut 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With