Skip to content

Instantly share code, notes, and snippets.

@javipolo javipolo/dockshell
Last active Nov 25, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.