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/917e9b07dafd90aefe1649a7363dee3b to your computer and use it in GitHub Desktop.

Select an option

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