Tail stdout from multiple Docker containers

I have a script that runs 10 containers in the background (option fig -d). I want to combine stdout or log / var / log from all of them. How can i do this?

Containers run using different files to build dockers, so I can’t docker target1 target2 target3

Docker logs

accept only one container as parameter.

I considered creating a volume from / var / log in all containers, matching them with a directory outside of dockers, making sure that the logs did not have a counter name, and not using bash tail -f *. But I would appreciate a more elegant solution.

+7
source share
2 answers

This bash script will do what you want:

Docker magazines

#!/bin/bash if [ $# -eq 0 ]; then echo "Usage: $(basename "$0") containerid ..." exit 1 fi pids=() cleanup() { kill "${pids[@]}" } trap cleanup EXIT while [ $# -ne 0 ] do (docker logs -f -t --tail=10 "$1"|sed -e "s/^/$1: /")& pids+=($!) shift done wait 

Using:

 $ docker-logs containerid1 containerid2 ... containeridN 

The output from this script contains each line of monitored logs with the addition of a container identifier.

The script runs in --follow mode and should be interrupted using Ctrl-C .

Please note that docker logs parameters are hardcoded in the script. If you need to be able to manage docker logs parameters from the command line, then you will need to parse the command line arguments (for example, using getopts ).

+6
source

Docker does not support yet 1.12. But I have a workaround through bash;

 docker ps | grep -w <filter-text> | for i in `awk '{ print $1 }'`; do docker logs -f --tail=30 $i & done 

I use docker roaming modes from 1.12 and deploying many replicas. Thus, all my containers contain common text that matches the name of the service. To cross out all my logs in a node docker , I use this on every node docker. filter-text will only filter my containers.

If you want to stop the tail, this works for me;

 pkill -f 'docker logs' 
0
source

All Articles