Last active
June 28, 2022 21:18
-
-
Save jaydansand/29744bd44d6ea68611e430a401cb7d14 to your computer and use it in GitHub Desktop.
Revisions
-
jaydansand revised this gist
Jun 28, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/sh # Author: Jay Dansand # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14 # Date: May 29 2019, modified June 28 2022 CONTAINER="$1" if [ -z "$CONTAINER" ]; then -
jaydansand revised this gist
Feb 9, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,4 +45,4 @@ else # image. It's a bit roundabit, but we use "sh -c" to launch our command. docker run -it --rm --entrypoint="sh" "$CONTAINER" -c "$SHELLCMD" fi fi -
jaydansand revised this gist
Jan 10, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/sh # Author: Jay Dansand, Technology Services, Lawrence University # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14 # Date: May 29 2019, modified Jan 10 2020 CONTAINER="$1" if [ -z "$CONTAINER" ]; then -
jaydansand revised this gist
Jan 10, 2020 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,6 @@ else IS_SERVICE="$?" if [ "$IS_SERVICE" -eq 0 ]; then echo "Executing shell in service \"$SERVICE\" via docker-compose..." docker-compose exec "$SERVICE" sh -c "$SHELLCMD" else # It's not running, start an image. -
jaydansand revised this gist
Jan 10, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/sh # Author: Jay Dansand, Technology Services, Lawrence University # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14 # Date: May 29 2019, modified Jan 07 2020 CONTAINER="$1" if [ -z "$CONTAINER" ]; then -
jaydansand revised this gist
Jan 7, 2020 . 1 changed file with 25 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,14 @@ #!/bin/sh # Author: Jay Dansand, Technology Services, Lawrence University # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14 # Date: May 29 2019, modified Jan 07 2019 CONTAINER="$1" if [ -z "$CONTAINER" ]; then echo "Usage: `basename $0` container|service [shell command]" echo " Executes shell command (default bash || sh) in a container. If CONTAINER" echo " is not a running Docker container (appearing in 'docker ls -a') then it" echo " will be tested as a docker-compose service or run as an image name." exit 1 fi @@ -20,25 +20,30 @@ fi # Is it a running container (i.e. use "exec") or an image? RESULTS=`docker container ls -a 2>/dev/null | cut -f1 -s -d ' ' | tail -n +2 | grep -ci "^$CONTAINER" 2>/dev/null` RUNNING="$?" if [ "$RUNNING" -eq 0 ]; then # It's running, use exec. if [ "$RESULTS" != "1" ]; then echo "Given container string matches more than one running container (results: $RESULTS)" exit 2 fi echo "Executing shell in $CONTAINER..." docker exec -it "$CONTAINER" sh -c "$SHELLCMD" else # Is it docker-compose-based? # In theory w/label com.docker.compose.service=$CONTAINER. # docker-compose ps --services | grep -P "^${CONTAINER}\$" SERVICE=`docker-compose ps --services 2>/dev/null | grep -P "^${CONTAINER}\$" 2>/dev/null` IS_SERVICE="$?" if [ "$IS_SERVICE" -eq 0 ]; then echo "Executing shell in service \"$SERVICE\" via docker-compose..." echo $SHELLCMD docker-compose exec "$SERVICE" sh -c "$SHELLCMD" else # It's not running, start an image. echo "Running shell in image $CONTAINER..." # Use --entrypoint to override any defined automatic command baked into the # image. It's a bit roundabit, but we use "sh -c" to launch our command. docker run -it --rm --entrypoint="sh" "$CONTAINER" -c "$SHELLCMD" fi fi -
jaydansand revised this gist
Jul 1, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ fi SHELLCMD="$2" if [ -z "$SHELLCMD" ]; then SHELLCMD='sh -c "if [ `which bash` ]; then bash; else sh; fi"'; fi # Is it a running container (i.e. use "exec") or an image? -
jaydansand revised this gist
Jun 11, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,7 @@ if [ "$RUNNING" -eq 0 ]; then # docker-compose exec "$SERVICE" "$SHELLCMD" # else echo "Executing shell in $CONTAINER..." docker exec -it "$CONTAINER" sh -c "$SHELLCMD" # fi else # It's not running, start an image. -
jaydansand revised this gist
Jun 11, 2019 . 1 changed file with 18 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,20 @@ #!/bin/sh # Author: Jay Dansand, Technology Services, Lawrence University # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14 # Date: 05/29/2019, modified 06/11/2019 CONTAINER="$1" if [ -z "$CONTAINER" ]; then echo "Usage: `basename $0` container [shell]" echo " Executes shell (default bash || sh) in a container. If CONTAINER is" echo " not a running Docker container (appearing in 'docker ls -a') then it" echo " will be run as an image name." exit 1 fi SHELLCMD="$2" if [ -z "$SHELLCMD" ]; then SHELLCMD='sh -c "bash || sh"'; fi # Is it a running container (i.e. use "exec") or an image? @@ -25,10 +26,19 @@ if [ "$RUNNING" -eq 0 ]; then echo "Given container string matches more than one running container (results: $RESULTS)" exit 2 fi # # Is it docker-compose-based? # SERVICE=`docker inspect "$CONTAINER" 2>/dev/null | grep -oP '(?<="com.docker.compose.service":\s")(.+?)(?=")'` # if [ "$?" -eq 0 ]; then # echo "Executing shell in $CONTAINER (service \"$SERVICE\") via docker-compose..." # docker-compose exec "$SERVICE" "$SHELLCMD" # else echo "Executing shell in $CONTAINER..." docker exec -it "$CONTAINER" "$SHELLCMD" # fi else # It's not running, start an image. echo "Running shell in image $CONTAINER..." # Use --entrypoint to override any defined automatic command baked into the # image. It's a bit roundabit, but we use "sh -c" to launch our command. docker run -it --rm --entrypoint="sh" "$CONTAINER" -c "$SHELLCMD" fi -
jaydansand created this gist
May 29, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ #!/bin/sh # Author: Jay Dansand, Technology Services, Lawrence University # Date: 05/29/2019 CONTAINER="$1" if [ -z "$CONTAINER" ]; then echo "Usage: `basename $0` container [shell]" echo " Executes shell (default /bin/sh) in a container. If CONTAINER is not a" echo " running Docker container (appearing in 'docker ls -a') then it will be" echo " run as an image name." exit 1 fi SHELLCMD="$2" if [ -z "$SHELLCMD" ]; then SHELLCMD='/bin/sh'; fi # Is it a running container (i.e. use "exec") or an image? RESULTS=`docker container ls -a 2>/dev/null | cut -f1 -s -d ' ' | tail -n +2 | grep -ci "^$CONTAINER" 2>/dev/null` RUNNING="$?" if [ "$RUNNING" -eq 0 ]; then # It's running, use exec. if [ "$RESULTS" != "1" ]; then echo "Given container string matches more than one running container (results: $RESULTS)" exit 2 fi echo "Executing shell in $CONTAINER..." docker exec -it "$CONTAINER" "$SHELLCMD" else # It's not running, start an image. echo "Running shell in image $CONTAINER..." docker run -it --rm "$CONTAINER" "$SHELLCMD" fi