Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janmalec/1d3ce3b5a7e746fba78143172f8011ba to your computer and use it in GitHub Desktop.
Save janmalec/1d3ce3b5a7e746fba78143172f8011ba to your computer and use it in GitHub Desktop.
How To Open Another Terminal/Bash Instance In A Running Docker Container as current user #gistblog #bash #docker

How To Open Another Terminal/Bash Instance In A Running Docker Container as current user

Add the following to your bashrc.

#Add another docker window
function dock()
{
  if [ "$1" == "-h" ]
  then
    #display help
    printf  "Attaches this window as a new terminal (bash) instance to a running docker container\n"
    printf  "Usage: 'dock' or 'dock 6548as846'\n"
    printf  "Accepts container name or id\n"
    printf  "If no ID is given, then attaches to first found process.\n"
  elif [ $# -eq 0 ]
  then
    #Get the first process
    dockerpid="$(docker ps -q | head -1)"
    echo $dockerpid
    sudo docker exec -i -t --user $USER $dockerpid /bin/bash
  elif [ "$1" != "-h" ]
  then
    #Open terminal in that process
    sudo docker exec -i -t --user $USER $1 /bin/bash
  fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment