Skip to content

Instantly share code, notes, and snippets.

@jaydansand
Last active June 28, 2022 21:18
Show Gist options
  • Select an option

  • Save jaydansand/29744bd44d6ea68611e430a401cb7d14 to your computer and use it in GitHub Desktop.

Select an option

Save jaydansand/29744bd44d6ea68611e430a401cb7d14 to your computer and use it in GitHub Desktop.

Revisions

  1. jaydansand revised this gist Jun 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions docker-shell.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/sh
    # Author: Jay Dansand, Technology Services, Lawrence University
    # Author: Jay Dansand
    # URL: https://gist.github.com/jaydansand/29744bd44d6ea68611e430a401cb7d14
    # Date: May 29 2019, modified Jan 10 2020
    # Date: May 29 2019, modified June 28 2022

    CONTAINER="$1"
    if [ -z "$CONTAINER" ]; then
  2. jaydansand revised this gist Feb 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-shell.sh
    Original 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
    fi
  3. jaydansand revised this gist Jan 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-shell.sh
    Original 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
    # Date: May 29 2019, modified Jan 10 2020

    CONTAINER="$1"
    if [ -z "$CONTAINER" ]; then
  4. jaydansand revised this gist Jan 10, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion docker-shell.sh
    Original 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..."
    echo $SHELLCMD
    docker-compose exec "$SERVICE" sh -c "$SHELLCMD"
    else
    # It's not running, start an image.
  5. jaydansand revised this gist Jan 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-shell.sh
    Original 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 2019
    # Date: May 29 2019, modified Jan 07 2020

    CONTAINER="$1"
    if [ -z "$CONTAINER" ]; then
  6. jaydansand revised this gist Jan 7, 2020. 1 changed file with 25 additions and 20 deletions.
    45 changes: 25 additions & 20 deletions docker-shell.sh
    Original 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: 05/29/2019, modified 06/11/2019
    # Date: May 29 2019, modified Jan 07 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."
    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
    # # 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" sh -c "$SHELLCMD"
    # fi
    echo "Executing shell in $CONTAINER..."
    docker exec -it "$CONTAINER" 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
    # 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
  7. jaydansand revised this gist Jul 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-shell.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ fi

    SHELLCMD="$2"
    if [ -z "$SHELLCMD" ]; then
    SHELLCMD='sh -c "bash || sh"';
    SHELLCMD='sh -c "if [ `which bash` ]; then bash; else sh; fi"';
    fi

    # Is it a running container (i.e. use "exec") or an image?
  8. jaydansand revised this gist Jun 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-shell.sh
    Original 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" "$SHELLCMD"
    docker exec -it "$CONTAINER" sh -c "$SHELLCMD"
    # fi
    else
    # It's not running, start an image.
  9. jaydansand revised this gist Jun 11, 2019. 1 changed file with 18 additions and 8 deletions.
    26 changes: 18 additions & 8 deletions docker-shell.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,20 @@
    #!/bin/sh
    # Author: Jay Dansand, Technology Services, Lawrence University
    # Date: 05/29/2019
    # 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 /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."
    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='/bin/sh';
    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
    echo "Executing shell in $CONTAINER..."
    docker exec -it "$CONTAINER" "$SHELLCMD"
    # # 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..."
    docker run -it --rm "$CONTAINER" "$SHELLCMD"
    # 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
  10. jaydansand created this gist May 29, 2019.
    34 changes: 34 additions & 0 deletions docker-shell.sh
    Original 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