Last active
May 4, 2021 07:47
-
-
Save VeikkoLehmuskorpi/917e9b07dafd90aefe1649a7363dee3b to your computer and use it in GitHub Desktop.
Search for a Docker container and enter an interactive bash shell
This file contains hidden or 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
| #!/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