Last active
July 16, 2022 20:52
-
-
Save mrsarm/c0020d93cfd8798df47d38d59b9ac94d to your computer and use it in GitHub Desktop.
docker-sh: start a shell session with the container passed
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
#!/usr/bin/env bash | |
if [ "$#" -ne 1 -o "$1" == "-h" -o "$1" == "--help" ]; then | |
echo 'docker-sh: start a shell session with the container passed' | |
echo | |
echo "Use: docker-sh CONTAINER_NAME" | |
echo | |
echo "NOTE: partial names can be used, but the session" | |
echo " is started only if one container match" | |
exit 2 | |
fi | |
CONTAINER=$(docker ps -f name=$1 -q) | |
NL=$'\n' | |
if [[ $CONTAINER == *$NL* ]]; then | |
echo "More than one container match the name \"$1\":" | |
echo | |
docker ps -f name=$1 --format "{{.Names}}" | |
echo | |
echo "Check available containers with 'docker ps -f name=NAME'." | |
exit 3 | |
fi | |
if [ "$CONTAINER" == "" ]; then | |
echo "No container match the name \"$1\", or your container exited." | |
echo "Check available and exited containers with 'docker ps -a -f name=NAME'." | |
exit 4 | |
fi | |
docker exec -it $CONTAINER sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment