Docker Menu Base Tool

As I mentioned before, I wrote a small script (for Linux) to help me with containers in my test lab.

This small script would help with “starting, stopping, removing, creating and listing” of containers.
For “starting” or “stopping” the containers, it’s possible to use their name or pattern.
For the creation of container, it’s possible to give a name and based on that and quantity provided in next step, the script could create one or multiple containers with that name or pattern.

Below is the script and here is the link to easily download it.

#!/bin/bash
ScriptName='Docker Menu Base Tool'
Ver='Ver 0.05'
Auther='@Kasraeian'

function rls() {
read waiting
clear
$0
}

clear
printf "============================================\n$ScriptName $Ver by $Auther\n============================================\n"
printf "Action list: \n\t1. Start\n\t2. Stop\n\t3. Remove\n\t4. Create\n\t5. List Lives\n\t6. List All\n\t0. Exit\n\t*. Relunch script\nPlease select an action #: "
read action

if [ "$action" = "1" ]; then #Start
docker ps -a --format '{{.ID}}\t{{.Names}}\t{{.Image}}'
printf "Select container name/pattern to start: "
read containername
if [ "$containername" = "" ]; then
echo "No container selected"
rls
else
echo "All container should be started? (Y/n)"
read allcontainerq
if [ "$allcontainerq" = "Y" ] || [ "$allcontainerq" = "y" ]; then
echo "Starting containers with '$containername' pattern ..."
docker start $(docker ps -a -q -f "name=$containername")
rls
elif [ "$allcontainerq" = "n" ]; then
echo "Starting '$containername' container ..."
docker start $containername
rls
else
echo "No valid answer received"
exit 0
fi
fi
elif [ "$action" = "2" ]; then #Stop
docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}'
printf "Select container name/pattern to stop: "
read containername
if [ "$containername" = "" ]; then
echo "No container selected"
rls
else
echo "All container should be stopped? (Y/n)"
read allcontainerq
if [ "$allcontainerq" = "Y" ] || [ "$allcontainerq" = "y" ]; then
echo "Stopping containers with '$containername' pattern ..."
docker stop $(docker ps -q -f "name=$containername")
rls
elif [ "$allcontainerq" = "n" ]; then
echo "Stopping '$containername' container ..."
docker stop $containername
els
else
echo "No valid answer received"
exit 0
fi
fi
elif [ "$action" = "3" ]; then #Remove
docker ps -a --format '{{.ID}}\t{{.Names}}'
printf "Select container name/pattern to remove: "
read containername
if [ "$containername" = "" ]; then
echo "No container selected"
rls
else
echo "All container should be removed? (Y/n)"
read allcontainerq
if [ "$allcontainerq" = "Y" ] || [ "$allcontainerq" = "y" ]; then
printf "Are you sure you want to DELETE these containers? Please enter \"YeS\" "
read accept
if [ "$accept" = "YeS" ]; then
echo "Removing containers with '$containername' pattern ..."
docker rm $(docker ps -a -q -f "name=$containername")
rls
else
echo "Nothing changed ..."
rls
fi
elif [ "$allcontainerq" = "n" ]; then
printf "Are you sure you want to DELETE these containers? Please enter \"YeS\" "
read accept
if [ "$accept" = "YeS" ]; then
echo "Removing '$containername' container ..."
docker rm $containername
rls
else
echo "Nothing changed ..."
rls
fi
else
echo "No valid answer received"
rls
fi
fi
elif [ "$action" = "4" ]; then #Create
printf "Tag\t\tSize\t\tRepository\n---------------------------------------------------------\n";docker images --format '{{.Tag}}\t\t{{.Size}}\t\t{{.Repository}}'
printf "Select image as source of container: "
read imagename
printf "Select container name/pattern to create: "
read containername
printf "Please select the quantity of containers: "
read containerqty
if [ "$imagename" = "" ] || [ "$containername" = "" ] || [ "$containerqty" = "" ] || [ "$containerqty" -lt 1 ]; then
echo "No quantity or image/container name provided."
rls
else
if [ "$containerqty" -gt 1 ]; then
echo "Creating $containerqty containers with '$containername' pattern ..."
i=1
while [[ $i -le $containerqty ]]
do
docker run -d --name $containername$i --hostname $containername$i -p 808$i:80 $imagename
((i = i + 1))
done
rls
elif [ "$containerqty" -eq 1 ]; then
echo "Creating '$containername' container ..."
docker run -d --name $containername $imagename
rls
fi
fi
elif [ "$action" = "5" ]; then #ListLives
printf "ID\t\tName\t\tImage\t\t\tPorts\t\t\tMounts\n-----------------------------------------------------------------------------------------------------\n"
docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Mounts}}'
read waiting
$0
elif [ "$action" = "6" ]; then #ListAll
printf "ID\t\tName\t\tImage\n---------------------------------------------------------\n"
docker ps -a --format '{{.ID}}\t{{.Names}}\t{{.Image}}'
read waiting
$0
elif [ "$action" = "0" ] || [ "$action" = "" ] || [ "$action" = "quit" ] || [ "$action" = "exit" ]; then #Exit
clear
echo "Thanks for using \"$ScriptName\" script."
exit 0
elif [ "$action" = "*" ]; then #Repeat
$0
elif [ "$action" = "clear" ] || [ "$action" = "clean" ]; then #clear
clear
$0
else
clear
$0
fi

Hope you find it useful.

It's your kindness to leave a reply/feedback