Skip to content

Instantly share code, notes, and snippets.

@efrecon
Created August 29, 2018 14:05
Show Gist options
  • Save efrecon/64ec348f9a1089b7c7714752b41a6c4d to your computer and use it in GitHub Desktop.
Save efrecon/64ec348f9a1089b7c7714752b41a6c4d to your computer and use it in GitHub Desktop.
Execut docker log on all containers at once
#!/bin/bash
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
@efrecon
Copy link
Author

efrecon commented Aug 29, 2018

This is shamelessly taken from https://stackoverflow.com/a/38308974

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment