Files
linux_docker/start_docker.sh
T
John Colvin 56c61231f3 Upgrade ubuntu to latest LTS (22.10)
This is a big upgrade, from 18.04!
2022-10-04 13:49:25 -05:00

124 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
function print_help_and_exit() {
cat <<EOF
Start a docker dev container
Usage: start_docker.sh [OPTIONS]
Options:
-i,--image The image to instantiate
-c,--container The name to give the container
-m,--mount The name for the folder (on the host) that is mounted to container's 'home' dir
-h,--help Print this help message and exit
EOF
exit $1
}
declare image_name=""
declare cont_name=""
declare mount_name=""
while [[ $# -gt 0 ]]; do
case "$1" in
-i)
;&
--image)
if [[ -z "$2" ]]; then
echo "Image (-i,--image) must be non-empty"
fi
image_name="$2"
shift 2
;;
-c)
;&
--container)
if [[ -z "$2" ]]; then
echo "Container (-c,--container) must be non-empty"
fi
cont_name="$2"
shift 2
;;
-m)
;&
--mount)
if [[ -z "$2" ]]; then
echo "Mount (-m,--mount) must be non-empty"
fi
mount_name="$2"
shift 2
;;
-h)
;&
--help)
print_help_and_exit 0
;;
*)
echo "Unexpected argument '$1'"
print_help_and_exit 1
;;
esac
done
if [[ -z "$image_name" ]]; then
image_name="bluetradingsystems/spark-crypto-dev"
fi
declare host_name="$(hostname)"
if [[ ! -z "$cont_name" ]]; then
host_name="$host_name.$cont_name"
fi
if [[ -z "$mount_name" ]]; then
mount_name="$cont_name"
fi
if [[ -z "$mount_name" && -z "$cont_name" ]]; then
echo "Please specify mount (-m,--mount) and/or container (-c,--container)"
print_help_and_exit 1
fi
declare home_src=$HOME/.spark-dev/mounts/$mount_name
declare home_tgt=$HOME
declare repo_src=$(git rev-parse --show-superproject-working-tree)
declare repo_tgt=$HOME/$(basename $repo_src)
declare working_dir=$HOME
if [[ -e $home_src ]]; then
# Don't overwrite existing dir (might be mounted in another container)
echo "Mount dir '$home_src' already exists"
print_help_and_exit 1
fi
mkdir -p $home_src
echo "Starting container
image_name : $image_name
cont_name : $cont_name
home_src : $home_src
home_tgt : $home_tgt
repo_src : $repo_src
repo_tgt : $repo_tgt
working dir : $working_dir"
docker run \
--net host \
-h "$host_name" \
-e "TZ=America/New_York" \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
--name "$cont_name" \
-v "$home_src:$home_tgt" \
-v "$repo_src:$repo_tgt" \
-u $(id -u $USER):$(id -g $USER) \
-w "$working_dir" \
--shm-size=4024m \
-t \
-i \
-d \
-e DISPLAY="$DISPLAY" \
"$image_name" \
bash