Skip to content

Instantly share code, notes, and snippets.

@lrivallain
Last active January 16, 2017 16:31
Show Gist options
  • Save lrivallain/41fb620609066c3cf892 to your computer and use it in GitHub Desktop.
Save lrivallain/41fb620609066c3cf892 to your computer and use it in GitHub Desktop.
dockerssh (easy ssh to a docker container)
##################################################
## Add the following lines to your .bashrc file ##
##################################################
# change terminal title (usefull for graphical ones)
# Usage:
# settitle "my beautifull title"
settitle(){
echo -n -e "\033]0;$1\007"
}
# SSH to a docker container according to the TCP/22
# port redirection
#
# start SSH session as current user:
# dockerssh mycontainer
# start SSH session as a specific user:
# dockerssh mycontainer myuser
dockerssh(){
if [[ $# -lt 1 ]]; then
echo "Not enough args: dockerssh container [specific_user] [container_ssh_port]"
else
container=$1
[ $# -gt 2 ] && port=$3 || port=22
sshport=`docker port $container $port`
if [[ $? -eq 0 ]]; then
sshport=`echo $sshport | awk -F ":" '{print $2}'`
settitle $container
[ $# -gt 1 ] && ssh $2@127.0.0.1 -p $sshport || ssh 127.0.0.1 -p $sshport
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment