Skip to content

Instantly share code, notes, and snippets.

@jasenmichael
Last active November 29, 2022 13:41
Show Gist options
  • Save jasenmichael/8cb1b0b2ab9bca5d3038b72b83044f2b to your computer and use it in GitHub Desktop.
Save jasenmichael/8cb1b0b2ab9bca5d3038b72b83044f2b to your computer and use it in GitHub Desktop.
run docker container, mount local volume, and open terminal.
#!/bin/bash
type docker >/dev/null 2>&1 || { echo "Docker could not be found"; exit 1; }
function usage()
{
cat << EOF
Usage: $progname [--port NUM] [--dir STR] [--image STR]
optional arguments:
-h, --help show this help message and exit
-p, --port NUM pass in a port to expose in the container *DEFAULT is 3000
-d, --dir STR pass in a directory to mount as a volume *DEFAULT is "./"
-i, --image STR pass in a image string *DEFAULT is "node:16"
-f, --forwardport NUM pass in a port to forward to on the host (forward to a different host port) *DEFAULT is \$port
-c, --command STR pass in a command string *DEFAULT is "bash"
EXAMPLES:
$progname -p 3000 -d ./ -i node:16 -f 3001
$progname --port=3000 --dir="./" --image=node:16 --forwardport=3005
EOF
}
dir=$PWD
port=3000
image=node:16
forwardport=false
command=bash
verbose=0
dryrun=0
filename=$(basename $0)
progname="${filename%%.*}"
OPTS=$(getopt -o "hd:p:i:c:v" --long "help,dir:,port:,image:,forwardport:command:,verbose,dry-run" -n "$progname" -- "$@")
if [ $? != 0 ] ; then echo "Error in command line arguments." >&2 ; usage; exit 1 ; fi
eval set -- "$OPTS"
while true; do
case "$1" in
-h | --help ) usage; exit; ;;
-d | --dir ) dir="$2"; shift 2 ;;
-p | --port ) port="$2"; shift 2 ;;
-i | --image ) image="$2"; shift 2 ;;
-f | --forwardport ) forwardport=$2; shift 2 ;;
-c | --command ) command=$2; shift 2 ;;
-v | --verbose ) verbose=1; shift ;;
--dry-run ) dryrun=1; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
[[ $forwardport == false ]] && forwardport=$port || forwardport=$forwardport
[[ $dir = /* ]] || dir=$PWD/$dir
container_name=boater-${image//:/-}
uid=$(id -u)
gid=$(id -g)
if [ $verbose == 1 ]; then
cat <<EOF
dir=$dir
port=$port
image=$image
forwardport=$forwardport
command=$command
verbose=$verbose
progname=$progname
container_name=$container_name
EOF
fi
user_opt=''
if docker version | grep -q 'Server: Docker Engine'; then
user_opt="-u $uid:$gid"
fi
final_command="docker run -it --rm -h $container_name -v $dir:$dir -w $dir -p $forwardport:$port $user_opt --init $image $command"
if [ $dryrun == 1 ]; then
echo "Dry run command:"
echo $final_command
exit 0
fi
echo "running $final_command"
$final_command
@jasenmichael
Copy link
Author

jasenmichael commented Nov 22, 2022

boater

this script runs a docker container, mounts the current directory (or specified dir) as a volume, and opens a terminal in the container.

*this script requires docker

download the script and make it executable with:

curl -sL https://s76.co/get-boater -o ~/boat.sh && chmod +x ~/boat.sh

see usage

~/boat.sh --help
  Usage: boat [--port NUM] [--dir STR] [--image STR]
  optional arguments:
    -h, --help               show this help message and exit
    -p, --port NUM           pass in a port to expose in the container *DEFAULT is 3000
    -d, --dir STR            pass in a directory to mount as a volume *DEFAULT is "./"
    -i, --image STR          pass in a image string *DEFAULT is "node:16"
    -f, --forwardport NUM    pass in a port to forward to on the host (forward to a different host port) *DEFAULT is $port
    -c, --command STR        pass in a command string *DEFAULT is "bash"
    --dry-run                print the docker command that would be run

  EXAMPLES:
    boat -p 3000 -d ./ -i node:16 --f 3001
    boat --port=3000 --dir="./" --image=node:16 --forwardport=3005

create an alias so you can run boat

echo "\nalias boat=~/boat.sh" >> ~/.bashrc && . ~/.bashrc 
# if you use zsh
echo "\nalias boat=~/boat.sh" >> ~/.zshrc && . ~/.zshrc

# check it worked
boat -h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment