[Bash] EZ container consoling from terminal


Recommended Posts

I made this bash script to simplify consoling my running docker containers via SSH/terminal. Apologies if this is nothing new, I didn't find one prior to making this topic.

 

I also used this method to keep the script persistent in my .bash_profile on Unraid 

 

The script:

 

function conn() { docker exec -it "$1" bash -c "grep @$1 ~/.bashrc >/dev/null || echo \"export PS1='\[\033[01;32m\]\u@$1\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\\\\\$ '\" >> ~/.bashrc ; bash" ; }
alias ds='dss'
function dss() {
  cons=$(docker ps --format '{{.Names}}')
  if [ "$cons" ]; then
    declare -A dvar=()
    c=1
    echo "Press a number to enter a container or 0 to exit"
    echo "    [0] Exit"
    for i in $cons
      do
      echo "    [$c] $i"
      dvar[$c]="$i"
      ((c=c+1))
    done
    ((c=c-1))
    echo "Input choice: "
    read input
    if [ "$input" == 0 ]; then
      return 0
    elif [ "$input" -ge 1 ] && [ "$input" -le "$c" ]; then
      conn "${dvar[$input]}"
    else
      return 0
    fi
  else
    echo "No running containers"
  fi
}

 

Here is some sample output

root@Gensokyo:~# ds
Press a number to enter a container or 0 to exit
    [0] Exit
    [1] quakejs
    [2] transmission
    [3] tubesync
    [4] Plex-Media-Server
Input choice: 
1
root@quakejs:/# exit
root@Gensokyo:~# 

 

The conn() function will append a PS1 to the ~/.bashrc file, but only if it hasn't done so already / if the line isn't there. 

I have tested this on all my containers without issue. I don't know if there are any weird containers out there where this may not work. If you run into an issue with the PS1 setting you can just remove the entire 'bash -c ...' bit from conn() and put literally just the word "bash"  instead (no quotes, though I suppose it may not matter). 

 

Any feedback or suggestions welcome, also keep in mind I'm extremely inexperienced in scripting. 

 

Here it is again with comments.

Spoiler



function conn() { docker exec -it "$1" bash -c "grep @$1 ~/.bashrc >/dev/null || echo \"export PS1='\[\033[01;32m\]\u@$1\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\\\\\$ '\" >> ~/.bashrc ; bash" ; }
# this function is what consoles the container. the bash -c does a couple things, it will first check for the existance of my PS1
#   using grep, the || bit equates to 'do this if previous command is false/empty' so this part runs if my PS1 was not found in .bashrc. The PS1 is added by appending itself to the end of the file. Afterwards, regardless of the prior PS1 check, run bash inside the container. Yes I'm sorry that was confusing.
alias ds='dss'
# call the script with ds
function dss() {
  cons=$(docker ps --format '{{.Names}}')
  # get the names of all the running containers
  if [ "$cons" ]; then
  # if the variable is not empty, meaning at least one container is running, do the following
    declare -A dvar=()
    # prepare a dynamic variable
    c=1
    # used for counting and setting dynamic vars
    echo "Press a number to enter a container or 0 to exit"
    echo "    [0] Exit"
    # echo first part of the menu
    for i in $cons
    # loop though each container name
      do
      echo "    [$c] $i"
      # print menu item
      dvar[$c]="$i"
      # set the dynamic var. eg $dvar1 plex-media-server
      ((c=c+1))
      # increment counter
    done
    ((c=c-1))
    # decrement counter so it corresponds with number of containers
    echo "Input choice: "
    read input
    # get user input
    if [ "$input" == 0 ]; then
      return 0
      # quit script if user input is 0
    elif [ "$input" -ge 1 ] && [ "$input" -le "$c" ]; then
    # if the input is valid, so the number if between 1 and whatever the last container is
      conn "${dvar[$input]}"
      # send the container name to conn() using dynamic var
    else
      return 0
      # if the input is not 0 and is not a valid number corresponding to a container, just quit the script
    fi
  else
    echo "No running containers"
    # if the $cons variable is empty / there are no containers running
  fi
}

 

 

Edited by hata
Just adding a note. And dec $c
Link to comment
  • hata changed the title to [Bash] EZ container consoling from terminal

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.