Skip to content

Instantly share code, notes, and snippets.

@EnoF
Last active June 15, 2016 16:34
Show Gist options
  • Save EnoF/b31279fbe988a13c60096d45204fbfa6 to your computer and use it in GitHub Desktop.
Save EnoF/b31279fbe988a13c60096d45204fbfa6 to your computer and use it in GitHub Desktop.
Docker UP
#!/bin/bash
function print_help_dup() {
echo " usage:"
echo " dup [-i|--image] (options)"
echo
echo " -i|--images required The name of the docker image you to load into your container"
echo " dup -i node"
echo " -cmd|--command optional Provide a different command to run"
echo " default: /bin/bash"
echo " dup -i -nt node"
echo " -nt|--no-term optional Provide if no terminal should be opened"
echo " default: false"
echo " dup -i -nt true node"
echo " -p|--ports optional Provide the ports to be forwarded"
echo " default: empty"
echo " dup -i -p 9000:9000 node"
echo " -v|--volume optional Volume to be mounted"
echo " default: $(pwd):/usr/src"
echo " dup -i -v $(pwd)/target:/usr/src/target node"
echo " -w|--workdir optional Runs the command at the specified directory in the container"
echo " default: /usr/src"
echo " dup -i -w /usr/src/target node"
echo
echo " examples:"
echo " # runs node in a container"
echo " dup -i node"
echo " # mounts the current dir under /usr/src/app"
echo " dup -i node -w /usr/src/app"
echo " # links the port range 9000 till 10000 from the container to the host"
echo " dup -i node -p 9000-10000:9000-10000"
}
function value_or() {
if [ -z ${1+x} ];
then
echo $2
else
echo $1
fi
}
function set_default_params() {
COMMAND=/bin/bash
INTERACTIVE=-it
VOLUME="-v $(pwd):/usr/src"
WORKDIR="-w /usr/src"
}
function dup() {
# If no arguments are provided display the `help` log
HELP=TRUE
VOLUMES=
PORTS=
set_default_params
while [[ $# -gt 1 ]]
do
key="$1"
HELP=FALSE
case $key in
-cmd|--command)
COMMAND="$2"
shift # past argument
;;
-i|--image)
IMAGE="$2"
shift # past argument
;;
-nt|--no-term)
INTERACTIVE=
shift # past argument
;;
-p|--ports)
PORTS="$PORTS -p $2"
shift # past argument
;;
-v|--volume)
VOLUMES="$VOLUMES -v $2"
shift # past argument
;;
-w|--workdir)
VOLUMES="$VOLUMES -v $(pwd):$2"
WORKDIR="-w $2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
if [ $HELP = "TRUE" ];
then
print_help_dup
else
# echo multi vars so that the VARS are not seen as one param
RESULT="docker run $INTERACTIVE $(echo $(value_or $VOLUMES $VOLUME)) $(echo $WORKDIR) $(echo $PORTS) $IMAGE $COMMAND"
echo "Running:"
echo " $RESULT"
$(echo $RESULT)
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment