Created
August 29, 2017 15:43
-
-
Save ypujante/e2e3dd0fc9150c6ac6c7cc4cc1139c8b to your computer and use it in GitHub Desktop.
Start a local web server (nginx) serving local files using docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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