Skip to content

Instantly share code, notes, and snippets.

@sdr01810
Last active January 13, 2018 02:32
Show Gist options
  • Save sdr01810/aeb8ce627c7a2ce24baf36a75bb5fe8a to your computer and use it in GitHub Desktop.
Save sdr01810/aeb8ce627c7a2ce24baf36a75bb5fe8a to your computer and use it in GitHub Desktop.
Spin up a TeamCity server as a Docker container.
#!/bin/bash
## Spin up a TeamCity server as a Docker container.
## By Stephen D. Rogers <inbox.c7r@steve-rogers.com>, 2018-01.
##
## Usage:
##
## teamcity-server.spin-up [--interactive|-i] [--restart policy] [--tty|-t] [listening_port]
##
## The TeamCity server listening port (on the container host) defaults to 8111.
##
## 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=teamcity-server
container_image=sdr01810/${container_name}:latest
p1h="${1:-8111}" # TeamCity server listening port on the host
p1c="8111" # TeamCity server listening port in the container
for d1h in /var/local/workspaces/teamcity-server/data ; do # TeamCity server data directory on the host
for d1c in /data/teamcity_server/datadir ; do # TeamCity server data directory in the container
for d2h in /var/local/workspaces/teamcity-server/logs ; do # TeamCity server logs directory on the host
for d2c in /opt/teamcity/logs ; do # TeamCity server logs 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
if false ; then
xx :
xx docker pull "$container_image"
fi
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment