Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Start a local web server (nginx) serving local files using docker
#!/bin/bash
PORT=80
WWW_DIR=`pwd`
###############################################################################
#
# Function Name : usage()
# Arguments : N/A
# Return Result : N/A, exit 0
#
###############################################################################
usage()
{
echo ""
echo " Usage: webserver.sh [-h] [-p port] [-d dir] start|stop"
echo ""
echo " -h : usage help"
echo " -p : webserver port (default to 80)"
echo " -d : location of where the webserver should serve pages (default to PWD)"
echo ""
exit 0
}
###############################################################################
#
# Function Name : start()
# Arguments : N/A
# Return Result : N/A
#
###############################################################################
start()
{
docker run --name webserver -v $WWW_DIR:/usr/share/nginx/html:ro -d -p $PORT:80 nginx
echo "Started webserver... Serving [$WWW_DIR] on port [$PORT]"
}
###############################################################################
#
# Function Name : stop()
# Arguments : N/A
# Return Result : N/A
#
###############################################################################
stop()
{
docker rm -f webserver
echo "Stopped webserver."
}
# get script options
while getopts "hd:p:" opt ; do
case $opt in
h ) usage
exit 0
;;
p )
PORT=$OPTARG
;;
d )
WWW_DIR=$OPTARG
;;
\? ) usage
exit 1
;;
esac
done
# correct the index so the command argument is always $1
shift $(($OPTIND - 1))
case $1 in
'start' ) start
;;
'stop' ) stop
;;
*) usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment