<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:

- Makes sure docker is available
- On Windows and Mac OSX, creates a docker machine if required
- Informs the user of the URL to access the container with a web browser
- Stops and removes containers from previous runs to avoid conflicts
- Mounts the present working directory to /home/user/work on Linux and Mac OSX
- Prints out the graphical app output log following execution
- Exits with the same return code as the graphical app

Options:

  -h             Display this help and exit.
  -c             Container name to use (default ${container}).
  -i             Image name (default ${image}).
  -p             Port to expose HTTP server (default ${port}). If an empty
                 string, the port is not exposed.
  -r             Extra arguments to pass to 'docker run'. E.g.
                 --env="APP=glxgears"
  -q             Do not output informational messages.
EOF
}

while [ $# -gt 0 ]; do
	case "$1" in
		-h)
			show_help
			exit 0
			;;
		-c)
			container=$2
			shift
			;;
		-i)
			image=$2
			shift
			;;
		-p)
			port=$2
			shift
			;;
		-r)
			extra_run_args="$extra_run_args $2"
			shift
			;;
		-q)
	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
		echo "Creating machine ${_VM}..."
		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"

_RUNNING=$(docker ps -a -q --filter "name=${container}")
if [ -n "$_RUNNING" ]; then
	echo "Stopping and removing the previous session..."
	echo ""
	docker stop $container >/dev/null
			echo "Creating machine ${vm}..."
		fi
		docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${vm}
echo ""
echo "Setting up the graphical application container..."
echo ""
echo "Point your web browser to ${_URL}"
echo ""

_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
running=$(docker ps -a -q --filter "name=${container}")
if [ -n "$running" ]; then
	if [ -z "$quiet" ]; then
docker run \
  -d \
  --name $container \
  ${_MOUNT_LOCAL} \
  $port_arg \
  $image >/dev/null

	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 script variable naming consistent.

<DFF> @@ -60,22 +60,22 @@ if [ $? -ne 0 ]; then
 	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
-		echo "Creating machine ${_VM}..."
-		docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${_VM}
+os=$(uname)
+if [ "${os}" != "Linux" ]; then
+	vm=$(docker-machine active 2> /dev/null || echo "default")
+	if ! docker-machine inspect "${vm}" &> /dev/null; then
+		echo "Creating machine ${vm}..."
+		docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${vm}
 	fi
-	docker-machine start ${_VM} > /dev/null
-    eval $(docker-machine env $_VM --shell=sh)
+	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"
+ip=$(docker-machine ip ${vm} 2> /dev/null || echo "localhost")
+url="http://${ip}:$port"
 
-_RUNNING=$(docker ps -a -q --filter "name=${container}")
-if [ -n "$_RUNNING" ]; then
+running=$(docker ps -a -q --filter "name=${container}")
+if [ -n "$running" ]; then
 	echo "Stopping and removing the previous session..."
 	echo ""
 	docker stop $container >/dev/null
@@ -85,13 +85,13 @@ fi
 echo ""
 echo "Setting up the graphical application container..."
 echo ""
-echo "Point your web browser to ${_URL}"
+echo "Point your web browser to ${url}"
 echo ""
 
-_PWD_DIR="$(pwd)"
-_MOUNT_LOCAL=""
-if [ "${_OS}" = "Linux" ] || [ "${_OS}" = "Darwin" ]; then
-	_MOUNT_LOCAL=" -v ${_PWD_DIR}:/home/user/work "
+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
@@ -101,7 +101,7 @@ fi
 docker run \
   -d \
   --name $container \
-  ${_MOUNT_LOCAL} \
+  ${mount_local} \
   $port_arg \
   $image >/dev/null
 

Make script variable naming consistent.