Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Created July 22, 2018 12:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PieterScheffers/eb133650bf018056a7f4d64d51cd0009 to your computer and use it in GitHub Desktop.
Save PieterScheffers/eb133650bf018056a7f4d64d51cd0009 to your computer and use it in GitHub Desktop.
Tail all running Docker containers
#!/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
@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