Skip to content

Instantly share code, notes, and snippets.

@VeikkoLehmuskorpi
Last active May 4, 2021 07:47
Show Gist options
  • Select an option

  • Save VeikkoLehmuskorpi/25f8a31f25cf22401afcce316a0c07ad to your computer and use it in GitHub Desktop.

Select an option

Save VeikkoLehmuskorpi/25f8a31f25cf22401afcce316a0c07ad to your computer and use it in GitHub Desktop.
Search for a Docker container and view its logs
#!/usr/bin/env bash
# Searches the output of "docker ps" matching a string,
# and views the logs of the first matching container
#
# Usage:
# ./dlogs.sh <search_string>
function dlogs() {
if [ -z "$1" ] ; then
echo "Usage: ./dlogs.sh <search_string>"
exit 1
fi
if docker ps | grep -i "$1" ; then
docker logs --follow --timestamps \
$(docker ps | grep -i "$1" | awk '{print $1}' | head -1)
else
echo "no containers found matching '$1'"
exit 1
fi
}
dlogs $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment