Skip to content

Instantly share code, notes, and snippets.

@sdr01810
Last active January 13, 2018 02:33
Show Gist options
  • Save sdr01810/44eee232b0e0344024dfb6830bb4cd24 to your computer and use it in GitHub Desktop.
Save sdr01810/44eee232b0e0344024dfb6830bb4cd24 to your computer and use it in GitHub Desktop.
Spin up an Nginx server as a Docker container.
#!/bin/bash
## Spin up a Nginx server as a Docker container.
## By Stephen D. Rogers <inbox.c7r@steve-rogers.com>, 2017-06.
##
## Usage:
##
## nginx.spin-up [--interactive|-i] [--restart policy] [--tty|-t] [listening_port]
##
## The Nginx listening port (on the container host) defaults to 80.
##
## See also:
##
## docker run --help
##
umask 0002
set -e -o pipefail
no_worries() {
echo 1>&2 "No worries; continuing."
}
qq() {
printf "%q" "$@"
}
xx() {
echo 1>&2 "+" "$@"
"$@"
}
##
run_options=
while [ $# -gt 0 ] ; do
case "$1" in
--interactive|-i)
run_options+="${run_options:+ }${1}"
shift 1 ; continue
;;
--restart)
run_options+="${run_options:+ }${1} $(qq "${2}")"
shift 2 ; continue
;;
--tty|-t)
run_options+="${run_options:+ }${1}"
shift 1 ; continue
;;
--)
shift 1 ; break
;;
-*)
echo 1>&2 "unrecognized option: ${1}"
exit 2
;;
*)
break
;;
esac
done
container_name=nginx
container_image=sdr01810/${container_name}:latest
p1h="${1:-80}" # nginx listening port on the host
p1c="80" # nginx listening port in the container
for d1h in /var/local/workspaces/nginx/html ; do # nginx static-content directory on the host
for d1c in /usr/share/nginx/html ; do # nginx static-content directory in the container
for d2h in /var/local/workspaces/nginx/conf.ref ; do # nginx initial reference configuration directory on the host
for d2c in /var/local/workspaces/nginx/conf.ref ; do # nginx initial reference configuration directory in the container
for d3h in /var/local/workspaces/nginx/modules.ref ; do # nginx initial reference modules directory on the host
for d3c in /var/local/workspaces/nginx/modules.ref ; do # nginx initial reference modules directory in the container
for dxh in "$d1h" "$d2h" "$d3h" ; do
# The container determines owner uid/gid for "$dxh" and below;
# seal off access to that subtree to just the superuser (root).
for dxh_parent in "$(dirname "$(dirname "$dxh")")" ; do
xx sudo mkdir -p "$dxh_parent"
xx sudo chown root:root "$dxh_parent"
xx sudo chmod 0770 "$dxh_parent"
xx sudo chmod g+s "$dxh_parent"
done;done
xx :
xx docker pull "$container_image"
xx :
xx docker stop "$container_name" || no_worries
xx docker rm --force "$container_name" || no_worries
xx :
xx eval "docker run --name $(qq "$container_name") -d \
-v $(qq "$d1h":"$d1c") -v $(qq "$d2h":"$d2c") \
-p $(qq "$p1h":"$p1c") ${run_options} $(qq "$container_image")"
done;done
done;done
done;done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment