Created
July 22, 2018 12:39
-
-
Save PieterScheffers/eb133650bf018056a7f4d64d51cd0009 to your computer and use it in GitHub Desktop.
Tail all running Docker containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Source: https://stackoverflow.com/questions/32076878/logging-solution-for-multiple-containers-running-on-same-host | |
# Creator: Nate | |
names=$(docker ps --format "{{.Names}}") | |
echo "tailing $names" | |
while read -r name | |
do | |
# eval to show container name in jobs list | |
eval "docker logs -f --tail=5 \"$name\" | sed -e \"s/^/[-- $name --] /\" &" | |
# For Ubuntu 16.04 | |
#eval "docker logs -f --tail=5 \"$name\" |& sed -e \"s/^/[-- $name --] /\" &" | |
done <<< "$names" | |
function _exit { | |
echo | |
echo "Stopping tails $(jobs -p | tr '\n' ' ')" | |
echo "..." | |
# Using `sh -c` so that if some have exited, that error will | |
# not prevent further tails from being killed. | |
jobs -p | tr '\n' ' ' | xargs -I % sh -c "kill % || true" | |
echo "Done" | |
} | |
# On ctrl+c, kill all tails started by this script. | |
trap _exit EXIT | |
# For Ubuntu 16.04 | |
#trap _exit INT | |
# Don't exit this script until ctrl+c or all tails exit. | |
wait |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
bash docker-tail-all.bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment