Last active
July 25, 2022 09:57
-
-
Save Pheebzer/0602ede4956f8dd8b48e20dfee104cfd to your computer and use it in GitHub Desktop.
Bash script for quick docker exec
This file contains 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 | |
PS3="Container to exec: " | |
IFS=$'\n' | |
OUTPUT=( $(docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Ports}}") ) | |
# if there will always be 1 line of output, even when to containers are running | |
if [[ $(wc -l <<< $"{OUTPUT}") -le 1 ]]; then | |
echo "No running containers found, exiting.." | |
exit 0 | |
fi | |
echo " ${OUTPUT[0]}" | |
select container in ${OUTPUT[@]:1} | |
do | |
CONTAINER_ID=$( echo ${container} | cut -d ' ' -f1 ) | |
if [[ $REPLY -gt 0 && $REPLY -le ${#OUTPUT[@]}-1 ]] ; then | |
break | |
fi | |
done | |
if [ ! -z $1 ] ; then | |
echo -e "Running '$@'\n" | |
docker exec -it ${CONTAINER_ID} "$@" | |
exit | |
fi | |
echo "Running /bin/bash" | |
if docker exec -it ${CONTAINER_ID} /bin/bash ; then | |
exit | |
else | |
echo "/bin/bash not available in container" | |
fi | |
echo "Running /bin/sh" | |
if docker exec -it ${CONTAINER_ID} /bin/sh ; then | |
exit | |
else | |
echo "/bin/sh not available in container" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment