-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·97 lines (78 loc) · 2.1 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash -e
DIRNAME=$(dirname "$0")
DOCKER_REPOSITORY="magnetikonline/nginx"
NGINX_CONF_DIR="/etc/nginx"
NGINX_DOCUMENT_ROOT_DIR="/srv/http"
NGINX_LOG_DIR="/var/log/nginx"
. "$DIRNAME/version"
function exitError {
echo "Error: $1" >&2
exit 1
}
function getPathCanonical {
readlink --canonicalize-existing --no-newline "$1" || :
}
function usage {
cat <<EOF >&2
Usage: $(basename "$0") [OPTION]...
-c DIR path to Nginx config mounted at $NGINX_CONF_DIR
-d DIR document root mounted at $NGINX_DOCUMENT_ROOT_DIR
-l DIR optional path for Nginx logs back to host, mounted at $NGINX_LOG_DIR
-h display help
EOF
exit 1
}
function main {
# read arguments
local hostNginxConfDir=
local hostNginxDocumentRootDir=
local hostNginxLogDir=
local optKey
while getopts :c:d:l:h optKey; do
case $optKey in
c)
hostNginxConfDir=$OPTARG
;;
d)
hostNginxDocumentRootDir=$OPTARG
;;
l)
hostNginxLogDir=$OPTARG
;;
h|*)
usage
;;
esac
done
# verify paths
if [[ -z $hostNginxConfDir ]]; then
exitError "no host path to Nginx config given"
fi
if [[ ! -d $hostNginxConfDir ]]; then
exitError "invalid host path to Nginx config of [$hostNginxConfDir]"
fi
if [[ ! -f "$hostNginxConfDir/nginx.conf" ]]; then
exitError "unable to locate nginx.conf at [$hostNginxConfDir/nginx.conf]"
fi
if [[ -z $hostNginxDocumentRootDir ]]; then
exitError "no path to document root given"
fi
if [[ ! -d $hostNginxDocumentRootDir ]]; then
exitError "invalid document root of [$hostNginxDocumentRootDir]"
fi
if [[ (-n $hostNginxLogDir) && (! -d $hostNginxLogDir) ]]; then
exitError "invalid host path for log files of [$hostNginxLogDir]"
fi
# run image
docker run \
--detach \
--publish 8080:80/tcp \
--publish 8443:443/tcp \
--rm \
--volume "$(getPathCanonical "$hostNginxConfDir"):$NGINX_CONF_DIR" \
--volume "$(getPathCanonical "$hostNginxDocumentRootDir"):$NGINX_DOCUMENT_ROOT_DIR" \
${hostNginxLogDir:+--volume "$(getPathCanonical "$hostNginxLogDir"):$NGINX_LOG_DIR"} \
--workdir "$NGINX_DOCUMENT_ROOT_DIR" \
"$DOCKER_REPOSITORY:$NGINX_VERSION"
}
main "$@"