<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 (default opengl)
  -i             Image name (default thewtex/opengl)
  -p             Port to expose HTTP server (default 6080), if an empty
                 string, the port is not exposed
EOF
}

container=opengl
image=thewtex/opengl
port=6080

while [ $# -gt 0 ]; do
	case "$1" in
}

while [ $# -gt 0 ]; do
	case "$1" in
		-h)
			show_help
			exit 0
			;;
		-c)
			container=$2
			shift
			;;
		-i)
			port=$2
			shift
			;;
		'?')
			show_help >&2
			exit 1
			;;
		-q)
			quiet=1
			;;
		*)
			show_help >&2
			exit 1
			;;
	esac
	shift
done


which docker 2>&1 >/dev/null
if [ $? -ne 0 ]; then
	echo "Error: the 'docker' command was not found.  Please install docker."
	exit 1
fi

os=$(uname)
if [ "${os}" != "Linux" ]; then
	vm=$(docker-machine active 2> /dev/null || echo "default")
	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
}

running=$(docker ps -a -q --filter "name=${container}")
if [ -n "$running" ]; then
	if [ -z "$quiet" ]; then
		echo "Stopping and removing the previous session..."
		echo ""
	fi
	cleanup
fi

if [ -z "$quiet" ]; then
	echo ""
	echo "Setting up the graphical application container..."
  --name $container \
  ${mount_local} \
  $port_arg \
  $image >/dev/null

result=$(docker wait $container)
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> Allow extra arguments to be passed to 'docker run'.

<DFF> @@ -15,17 +15,20 @@ thewtex/opengl. It:
 
 Options:
 
-  -h             Display this help and exit
-  -c             Container name to use (default opengl)
-  -i             Image name (default thewtex/opengl)
-  -p             Port to expose HTTP server (default 6080), if an empty
-                 string, the port is not exposed
+  -h             Display this help and exit.
+  -c             Container name to use (default opengl).
+  -i             Image name (default thewtex/opengl).
+  -p             Port to expose HTTP server (default 6080). If an empty
+                 string, the port is not exposed.
+  -r             Extra arguments to pass to 'docker run'. E.g.
+                 --env="GRAPHICAL_APP=glxgears"
 EOF
 }
 
 container=opengl
 image=thewtex/opengl
 port=6080
+extra_run_args=""
 
 while [ $# -gt 0 ]; do
 	case "$1" in
@@ -45,6 +48,10 @@ while [ $# -gt 0 ]; do
 			port=$2
 			shift
 			;;
+		-r)
+			extra_run_args="$extra_run_args $2"
+			shift
+			;;
 		'?')
 			show_help >&2
 			exit 1
@@ -103,6 +110,7 @@ docker run \
   --name $container \
   ${mount_local} \
   $port_arg \
+  $extra_run_args \
   $image >/dev/null
 
 result=$(docker wait $container)

Allow extra arguments to be passed to 'docker run'.