nowk
10/25/2015 - 6:19 AM

Basic command template around `docker run` to bootstrap some basic options

Basic command template around docker run to bootstrap some basic options

#!/bin/sh
set -e

if [ ! -n "$1" ] ; then
	echo
	cat <<EOF
  USAGE:

    ./docker-run <command> [-- <docker options>]

  COMMANDS:

    <command>    Your command

  OPTIONS:

    -- <docker options>    Options to be passed to the Docker \`run\` command

    The --rm flag is automatically set as an option unless the --detach flag is true.
EOF
	echo
	return 0
fi

image='image/name:version' # TODO set this to your image

rmflag=true
volume= # TODO set default volumes

cmd=
opt=

# build the command and break at --
while [ "$#" -gt 0 ] ; do
	case "$1" in
	--)
		shift
		break
	;;
	*)
		cmd="$cmd $1"
	;;
	esac
	shift
done

# build docker options, everthing after --
while [ "$#" -gt 0 ] ; do
	case "$1" in
	-d|--detach|--detach=true)
		opt="$opt $1"
		rmflag=false
	;;
	*)
		opt="$opt $1"
	;;
	esac
	shift
done

# check to see if we should ad the --rm flag to options
if [ "$rmflag" = true ] && [ "$CIRCLE_CI" != true ] ; then
	opt="$opt --rm"
fi

exec /bin/sh -c "docker run ${volume} ${opt} -it ${image} ${cmd}"

# vim: filetype=sh