<NME> run.sh
<BEF> #!/bin/bash

container=opengl
image=thewtex/opengl
port=6080
extra_run_args=""
quiet=""

show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-q] [-c CONTAINER] [-i IMAGE] [-p PORT] [-r DOCKER_RUN_FLAGS]

This script is a convenience script to run Docker images based on
thewtex/opengl. It:

Options:

  -h             Display this help and exit
  -c             Container name to use
  -i             Image name
EOF
}

container=opengl
image=thewtex/opengl

OPTIND=1
while getopts "obs:h" opt; do
  case "$opt" in
    h)
      show_help
      exit 0
      ;;
    c)
      container=$OPTARG
      ;;
    i)
      image=$OPTARG
      ;;
    '?')
      show_help >&2
      exit 1
      ;;
  esac
done


			extra_run_args="$extra_run_args $2"
			shift
			;;
		-q)
			quiet=1
			;;
		*)
			show_help >&2
			exit 1
			;;
	esac
	shift
done


fi

_IP=$(docker-machine ip ${_VM} 2> /dev/null || echo "localhost")
_URL="http://${_IP}:6080"

_RUNNING=$(docker ps -a -q --filter "name=$container")
if [ -n "$_RUNNING" ]; then
	echo "Stopping and removing the previous session..."
	echo ""
	if ! docker-machine inspect "${vm}" &> /dev/null; then
		if [ -z "$quiet" ]; then
			echo "Creating machine ${vm}..."
		fi
		docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${vm}
	fi
	docker-machine start ${vm} > /dev/null
    eval $(docker-machine env $vm --shell=sh)
fi

ip=$(docker-machine ip ${vm} 2> /dev/null || echo "localhost")
url="http://${ip}:$port"

cleanup() {
	docker stop $container >/dev/null
	docker rm $container >/dev/null
  -d \
  --name $container \
  ${_MOUNT_LOCAL} \
  -p 6080:6080 \
  $image >/dev/null

result=$(docker wait $container)
	fi
	cleanup
fi

if [ -z "$quiet" ]; then
	echo ""
	echo "Setting up the graphical application container..."
	echo ""
	if [ -n "$port" ]; then
		echo "Point your web browser to ${url}"
		echo ""
	fi
fi

pwd_dir="$(pwd)"
mount_local=""
if [ "${os}" = "Linux" ] || [ "${os}" = "Darwin" ]; then
	mount_local=" -v ${pwd_dir}:/home/user/work "
fi
port_arg=""
if [ -n "$port" ]; then
	port_arg="-p $port:6080"
fi

docker run \
  -d \
  --name $container \
  ${mount_local} \
  $port_arg \
  $extra_run_args \
  $image >/dev/null

print_app_output() {
	docker cp $container:/var/log/supervisor/graphical-app-launcher.log - \
		| tar xO
	result=$(docker cp $container:/tmp/graphical-app.return_code - \
		| tar xO)
	cleanup
	exit $result
}

trap "docker stop $container >/dev/null && print_app_output" SIGINT SIGTERM

docker wait $container >/dev/null

print_app_output

# vim: noexpandtab shiftwidth=4 tabstop=4 softtabstop=0

<MSG> Make the port optional in run.sh

<DFF> @@ -16,32 +16,40 @@ thewtex/opengl. It:
 Options:
 
   -h             Display this help and exit
-  -c             Container name to use
-  -i             Image name
+  -c             Container name to use (default opengl)
+  -i             Image name (default thewtex/opengl)
+  -p             Port to expose HTTP server (default 6080)
 EOF
 }
 
 container=opengl
 image=thewtex/opengl
-
-OPTIND=1
-while getopts "obs:h" opt; do
-  case "$opt" in
-    h)
-      show_help
-      exit 0
-      ;;
-    c)
-      container=$OPTARG
-      ;;
-    i)
-      image=$OPTARG
-      ;;
-    '?')
-      show_help >&2
-      exit 1
-      ;;
-  esac
+port=6080
+
+while [ $# -gt 1 ]; do
+	case "$1" in
+		-h)
+			show_help
+			exit 0
+			;;
+		-c)
+			container=$2
+			shift
+			;;
+		-i)
+			image=$2
+			shift
+			;;
+		-p)
+			port=$2
+			shift
+			;;
+		'?')
+			show_help >&2
+			exit 1
+			;;
+	esac
+	shift
 done
 
 
@@ -63,9 +71,9 @@ if [ "${_OS}" != "Linux" ]; then
 fi
 
 _IP=$(docker-machine ip ${_VM} 2> /dev/null || echo "localhost")
-_URL="http://${_IP}:6080"
+_URL="http://${_IP}:$port"
 
-_RUNNING=$(docker ps -a -q --filter "name=$container")
+_RUNNING=$(docker ps -a -q --filter "name=${container}")
 if [ -n "$_RUNNING" ]; then
 	echo "Stopping and removing the previous session..."
 	echo ""
@@ -88,7 +96,7 @@ docker run \
   -d \
   --name $container \
   ${_MOUNT_LOCAL} \
-  -p 6080:6080 \
+  -p $port:6080 \
   $image >/dev/null
 
 result=$(docker wait $container)

Make the port optional in run.sh