Last active
February 14, 2026 19:58
-
-
Save ljm42/2b3bfd8ff886015bbce8 to your computer and use it in GitHub Desktop.
A script to easily "docker exec" into a running Docker container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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
Maybe I'am wrong, but if the container has only sh and not additionally bash, this script wont work.
I get an error and the execution ist stopped.
Is is an "php:8.4.18-fpm-alpine3.23" image and alpine does not have bash (without explicit installation). (only sh)
Is there a way to safely detect available shell in container?