A Handy Script to Join your Docker Containers


Recommended Posts

I got so sick of cutting and pasting commands like the below to enter into a specific container:

 

docker exec -t -i 37fcc75e886d /bin/bash

 

So I manually created a script that, with some editing on your part, you easily enter into the command line of your various dockers. I called my script "menu") and by typing one of the numbers, I can enter the container.

 

4f5xTuL.png

 

2EPIPDs.png

 

In order to edit the script to use the containers you have, type:

 

docker ps

 

Make note of the names of the containers you want all the way on the left hand column. These are the names you have to edit on the script below to easily activate.

 

#!/bin/bash
# This script will test if you have given a leap year or not.
clear
set -e

#Path to php binary
export PHP_PATH="/usr/bin/php"

#Path to your romance directory
export NEWZNAB_DIR="/var/www/nZEDb/misc"



echo " "
echo " "
echo -e '\E[37;44m'"\033[1m                                      \033[0m"
echo -e '\E[33;44m'"\033[1m        Docker Activation Menu        \033[0m"
echo -e '\E[37;44m'"\033[1m                                      \033[0m"
echo  " "
echo -e "\033[1;33;40m    Please type number of activity you would like to perform:\033[1;33;40m"
echo " "
echo -e "        1. \033[0;32;40m Calibre   \033[1;33;40m"
echo -e "        2. \033[0;32;40m Apache-PHP-phpMyAdmin   \033[1;33;40m"
echo -e "        3. \033[0;32;40m MariaDB   \033[1;33;40m"
echo -e "        4. \033[0;32;40m Dropbox \033[1;33;40m "


echo -e " "
echo " "
echo -e "\033[5m Type number and [ENTER]: "

tput sgr0

echo " "


read mychoice
tput sgr0


echo " "
if [ "$mychoice" = 1 ]
        then
                clear
                echo " "
                echo -e '\E[30;42m'"\033[1m                         \033[0m"
        echo -e '\E[30;42m'"\033[1m    Calibre Container    \033[0m"
                echo -e '\E[37;42m'"\033[1m                         \033[0m"
                echo  " "
                docker exec -it CalibreServer bash

elif [ "$mychoice" = 2 ]
        then
                clear
                echo " "
                echo -e '\E[30;42m'"\033[1m                           \033[0m"
                echo -e '\E[30;42m'"\033[1m   Apache-PHP Container    \033[0m"
                echo -e '\E[37;42m'"\033[1m                           \033[0m"
                echo  " "
          docker exec -it Apache-PHP-Adminer bash

elif [ "$mychoice" = 3 ]
        then
                clear
                echo " "
                echo -e '\E[30;42m'"\033[1m                          \033[0m"
        echo -e '\E[30;42m'"\033[1m    MariaDB Container     \033[0m"
                echo -e '\E[37;42m'"\033[1m                          \033[0m"
               echo  " "
        docker exec -it MariaDB bash

elif [ "$mychoice" = 4 ]
        then
                clear
                echo " "
                echo -e '\E[30;42m'"\033[1m                            \033[0m"
                echo -e '\E[30;42m'"\033[1m     Dropbox Container      \033[0m"
                echo -e '\E[37;42m'"\033[1m                            \033[0m"
                echo  " "
               docker exec -it Dropbox bash


else
        echo -e "\033[1;35;40mYou must enter a number between 1 and 6. \033[1;33;41m Exiting! "
        tput sgr0
fi

 

Put this script somewhere on your unRAID path to activate from any folder. I followed this thread on how to add folders to your paths. I have not tested as I am in the middle of a pre-clear and cannot reboot.

http://lime-technology.com/forum/index.php?action=post;quote=70080;topic=7220.0;last_msg=70116

 

I wish there was a way to automatically do this, but this is as far as I can go with my skills.

 

I hope this makes sense and that it is helpful.

Link to comment

Funny....  ;D  ;D to be honest... I was running an NZB indexer and I indexed ebooks... There were TONS of romance and erotic novels, and I created scripts to filter those out and erase them... you can see the scripts in my Github repo... I recycled my old script for the docker connection and I guess I left some old code in there.

 

 

Link to comment

I wish there was a way to automatically do this

 

Turns out it is really hard to parse the output of "docker ps" to get the names of all the containers :)  I spent way too much time Googling tr/sed/rev/cut/sort and finally came up with a solution that worked:

  https://gist.github.com/ljm42/2b3bfd8ff886015bbce8

 

This will present you with a list of all the running containers and "docker exec" into the one you choose.  Thanks for getting me started on this, it should make it a lot easier to move between containers.

Link to comment

I wish there was a way to automatically do this

 

Turns out it is really hard to parse the output of "docker ps" to get the names of all the containers :)  I spent way too much time Googling tr/sed/rev/cut/sort and finally came up with a solution that worked:

  https://gist.github.com/ljm42/2b3bfd8ff886015bbce8

 

This will present you with a list of all the running containers and "docker exec" into the one you choose.  Thanks for getting me started on this, it should make it a lot easier to move between containers.

 

Perhaps this can help you:

 

docker ps | awk 'NR==1 {offset=index($0,"NAMES")};NR>1{print substr($0,offset)}'

 

It finds the index position of the name in the first line, and prints all subsequent lines from that position ... works like a treat!

 

Link to comment

Perhaps this can help you:

 

docker ps | awk 'NR==1 {offset=index($0,"NAMES")};NR>1{print substr($0,offset)}'

 

It finds the index position of the name in the first line, and prints all subsequent lines from that position ... works like a treat!

 

Very interesting! I didn't think the columns lined up well enough for the NAMES data to always start in the same position, but it seems it does.  That awk solution is nice, thanks!

Link to comment

Perhaps this can help you:

 

docker ps | awk 'NR==1 {offset=index($0,"NAMES")};NR>1{print substr($0,offset)}'

 

It finds the index position of the name in the first line, and prints all subsequent lines from that position ... works like a treat!

 

Very interesting! I didn't think the columns lined up well enough for the NAMES data to always start in the same position, but it seems it does.  That awk solution is nice, thanks!

 

I don't know if NAMES is always in the same position, that's why I let awk find the position dynamically and use that to print the docker names.

 

Link to comment
  • 3 weeks later...

This is a great tool.

I tried to combine the script and the parser and ended up with this:

 

I hope you don't mind me playing with this code.

Did not test it with dockers that have blanks in the name. (Not sure that is possible)

 

#!/bin/bash
###############################################
# This script will let you chose which docker container to exec into
#
# Original idea and concept by hernandito
# DockerList by bonienl
#
################################################

clear
set -e

#Define Colours
ti=`tput setab 4;tput setaf 3`  #Title Yellow on Blue
ye=`tput setaf 3`               #Yellow on default
gr=`tput setaf 2`               #Green  on default
mn=`tput setaf 4;tput setab 7`  #menu
he=`tput setaf 0;tput setab 2`  #Heading New Docker

d=`tput sgr0`                   #reset to default


# rpad function needed in Docker Header
# rpad String Len Pad
function rpad {
  word="$1"
  while [ ${#word} -lt $2 ]; do
    word="$word$3";
  done;
  echo "$word";
}


# find all running dockers
DockerList=($(docker ps | awk 'NR==1 {offset=index($0,"NAMES")};NR>1{print substr($0,offset)}'))

# Display Menu
echo " "
echo " "
echo -e "${yb}                                       ${d}"
echo -e "${yb}      Docker Selector                  ${d}"
echo -e "${yb}                                       ${d}"
echo  " "
echo " "

for i in `seq 1 ${#DockerList[@]}`
do
  echo -e "${ye}        "$i". ${gr}"${DockerList[$i-1]}"${d}"
done

echo " "
echo -e "${ye}        0. ${gr}Exit - do nothing   ${d}"
echo " "
echo -e "${me} Type number and [ENTER]{d} "
echo " "

read mychoice
echo "${d} "

if [ "$mychoice" = 0 ]
  then
    clear
    exit 0
elif [ "$mychoice" -gt ${#DockerList[@]} ]
  then
    echo -e "You must enter a number between 0 and "${#DockerList[@]}" Exiting! ${d}"
    exit 0
else

## Connect to Docker
  clear
  echo -e "${he}                                               ${d}"
  echo -e "${he}     `rpad ${DockerList[$mychoice-1]} 41 " "` ${d}"
  echo -e "${he}                                               ${d}"
  echo " "
  docker exec -it ${DockerList[$mychoice-1]} bash

fi

exit 0

 

https://raw.githubusercontent.com/Data-Monkey/unRAID/master/tools/DockerSelector.sh

 

Link to comment

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.