Skip to content

Instantly share code, notes, and snippets.

@shotgunner
Last active May 30, 2020 06:14
Show Gist options
  • Save shotgunner/e794b227b0ee225176250c4ee7e92800 to your computer and use it in GitHub Desktop.
Save shotgunner/e794b227b0ee225176250c4ee7e92800 to your computer and use it in GitHub Desktop.
simplified `docker exec -it <some_container> bash` to a alias function in bash
GREEN='\033[0;32m'
NC='\033[0m' # No Color
function goto() {
program="bash"
if [ -n "$2" ]
then
program="$2"
fi
if [ $(docker ps -q --filter "name=$1" | wc -l) -gt 1 ]
then
echo "too many containers exists with this name !"
elif [ $(docker ps -q --filter "name=$1" | wc -l) -lt 1 ]
then
echo "no container found !"
else
echo "We are going to this container => ${GREEN}" $(docker ps -q --filter "name=$1" --format '{{.Names}}') "${NC}"
docker exec -it $(docker ps -q --filter "name=$1") $program
fi
}
@shotgunner
Copy link
Author

shotgunner commented Feb 25, 2020

1- Add this function to end of your .bashrc file (or .zshrc or others)
2- run source ~/.bashrc
3- use goto <part_of_the_name_of_container>

NOTE: this command use bash as default command but you can use sh or something else as second parameter like this:
goto <part_of_the_name_of_container> sh

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