Skip to content

Instantly share code, notes, and snippets.

@sdr01810
Last active June 28, 2017 20:42
Show Gist options
  • Save sdr01810/9c9e3ff4b06f47ac210e4ce132c80797 to your computer and use it in GitHub Desktop.
Save sdr01810/9c9e3ff4b06f47ac210e4ce132c80797 to your computer and use it in GitHub Desktop.
Spin up a Jenkins server as a Docker container.
#!/bin/bash
## Spin up a Jenkins server as a Docker container.
## By Stephen D. Rogers <inbox.c7r@steve-rogers.com>, 2017-04.
##
## Usage:
##
## jenkins.spin-up [--interactive|-i] [--restart policy] [--tty|-t] [listening_port [cli_listening_port]]
##
## The Jenkins listening port (on the container host) defaults to 8002.
## The Jenkins CLI listening port (on the container host) defaults to 2202.
##
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=jenkins
container_image=sdr01810/${container_name}:latest
p1h="${1:-8002}" # jenkins listening port on the host
p1c="8080" # jenkins listening port in the container
p2h="${2:-2202}" # jenkins CLI listening port on the host
p2c="2202" # jenkins CLI listening port in the container
for p3h in 50000 ; do # master/slave listening port on the host
for p3c in 50000 ; do # master/slave listening port in the container
for d1h in /var/local/workspaces/jenkins/home ; do # jenkins data directory on the host
for d1c in /var/jenkins_home:z ; do # jenkins data directory in the container
for d2h in /var/local/workspaces/jenkins/home.ref ; do # jenkins initial reference data directory on the host
for d2c in /usr/share/jenkins/ref:z ; do # jenkins initial reference data directory in the container
for dxh in "$d1h" "$d2h" ; 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") \
-p $(qq "$p2h":"$p2c") -p $(qq "$p3h":"$p3c") ${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