Skip to content

Instantly share code, notes, and snippets.

@javipolo
Last active November 25, 2016 17:40
Show Gist options
  • Save javipolo/a754265b83bef9925add5c046094c9ac to your computer and use it in GitHub Desktop.
Save javipolo/a754265b83bef9925add5c046094c9ac to your computer and use it in GitHub Desktop.
runs a shell on a docker container, searching it by name
# dockshell container [command] # Runs bash (or command) in container specified by name
dockshell(){
default='bash'
if [ $# -gt 0 ]; then
cmd=${2:-$default}
C_ID=$(sudo docker ps | awk '{if ( $2 == "'$1'") print $1}')
sudo docker exec -it $C_ID $cmd
else
echo "ERROR. Need container name"
fi
}
# Bash completion
_dockshell(){
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="$(sudo docker ps | awk '{print $NF}'|grep -vx NAMES)"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _dockshell dockshell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment