Skip to content

Instantly share code, notes, and snippets.

@ljm42
Last active March 20, 2024 07:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ljm42/2b3bfd8ff886015bbce8 to your computer and use it in GitHub Desktop.
Save ljm42/2b3bfd8ff886015bbce8 to your computer and use it in GitHub Desktop.
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