Skip to content

Instantly share code, notes, and snippets.

@Pheebzer
Last active July 25, 2022 09:57
Show Gist options
  • Save Pheebzer/0602ede4956f8dd8b48e20dfee104cfd to your computer and use it in GitHub Desktop.
Save Pheebzer/0602ede4956f8dd8b48e20dfee104cfd to your computer and use it in GitHub Desktop.
Bash script for quick docker exec
#!/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