Skip to content

Instantly share code, notes, and snippets.

@szz
Created May 28, 2020 23:20
Show Gist options
  • Save szz/5dad270ac6705fba5a69d70a5b02787a to your computer and use it in GitHub Desktop.
Save szz/5dad270ac6705fba5a69d70a5b02787a to your computer and use it in GitHub Desktop.
get the prefered interactive shell in a given docker conatiner
#!/bin/bash
SHELL_PREFERENCE=("bash zsh sh")
if [ -z "$1" ]; then
echo "No conatiner name supplied"
exit 255
fi
FOUND_CONTAINERS=$(docker container ls --format 'table {{.Names}}\t{{.ID}}' | grep -v "CONTAINER ID" | grep -w ${1})
EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne "0" ]; then
echo '"'${1}'" is not valid container'
docker container ls --format 'table {{.Names}}\t{{.ID}}'
exit 254
fi
for SHELL in ${SHELL_PREFERENCE}
do
docker exec -it ${1} ls -1 /bin/ > .doci.tmp
TMP=$(cat .doci.tmp | grep ${SHELL})
EXIT_CODE=$?
if [ "${EXIT_CODE}" -eq "0" ]; then
rm .doci.tmp
docker exec -it ${1} ${SHELL}
exit $?
fi
done
rm .doci.tmp
echo "There is no supported shell in the container"
exit 253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment