Skip to content

Instantly share code, notes, and snippets.

@ljm42
Last active Sep 19, 2021
Embed
What would you like to do?
A script to easily "docker exec" into a running Docker container
#!/bin/bash
# easily "docker exec" into a running Docker container
# latest version: https://gist.github.com/ljm42/2b3bfd8ff886015bbce8
# for unRAID, place this script on your flash drive as /boot/custom/docker-shell
# then add this to your go script (without the leading pound sign):
# cp /boot/custom/docker-shell /usr/local/bin
CONTAINERS=`docker ps | awk 'NR==1 {offset=index($0,"NAMES")};NR>1{print substr($0,offset)}' | sort -f | tr "\n" " "`
echo
echo "Choose a Docker container:"
i=1
for container in $CONTAINERS
do
echo "$((i++)) : $container"
done
read MYCHOICE
CHOSEN=`echo $CONTAINERS | cut -d' ' -f$MYCHOICE 2>/dev/null`
if [ "$CHOSEN" = "" ]; then
echo Invalid option
exit
fi
# try running bash, if exit code 126 then try running sh
for SHELL in bash sh
do
clear
echo " "
echo -e '\E[30;42m'"\033[5m $CHOSEN - $SHELL \033[0m"
echo " "
tput sgr0
docker exec -it $CHOSEN $SHELL
if [ $? -ne 126 ]; then
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment