Files
linux_docker/dev/build.sh

124 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
function print_help_and_exit() {
cat <<EOF
Build the (dev) docker image
Usage: build.sh [OPTIONS]
Options:
-u,--user USER or USER:UID The user to add to the image (defaults to the current user)
-p,--pass The password for the <--user> in the image (defaults to the username in the
image)
-i,--image The name to give the image (defaults to "USER:spark-dev")
-h,--help Print this help message and exit
EOF
exit "$1"
}
declare docker_user=""
declare docker_pass=""
declare image_name=""
declare -i docker_pull=1
while [[ $# -gt 0 ]]; do
case "$1" in
-i)
;&
--image)
if [[ -z "$2" ]]; then
echo "Image (-i,--image) must be non-empty"
print_help_and_exit 1
fi
image_name="$2"
shift 2
;;
-u)
;&
--user)
if [[ -z "$2" ]]; then
echo "User (-u,--user) must be non-empty"
print_help_and_exit 1
fi
docker_user="$2"
shift 2
;;
-p)
;&
--pass)
if [[ -z "$2" ]]; then
echo "Password (-p,--pass) must be non-empty"
print_help_and_exit 1
fi
docker_pass="$2"
shift 2
;;
--no-pull)
docker_pull=0
shift 1
;;
-h)
;&
--help)
print_help_and_exit 0
;;
*)
echo "Unexpected argument '$1'"
print_help_and_exit 1
;;
esac
done
declare -a docker_user_args=($(echo "$docker_user" | sed -e 's/:/ /g'))
case ${#docker_user_args[@]} in
0)
docker_user="$USER"
docker_uid="$(id -u)"
;;
1)
docker_user="${docker_user_args[0]}"
docker_uid="$(id -u)"
;;
2)
docker_user="${docker_user_args[0]}"
docker_uid="${docker_user_args[1]}"
;;
*)
echo "Invalid docker_user: $docker_user"
print_help_and_exit 1
;;
esac
if [[ $docker_pull -eq 1 ]]; then
# Pull the base image from Docker Hub
base_image_name="johncolvin/bluetradingsystems:spark-dev"
echo "Pulling image '$base_image_name' from Docker Hub"
docker pull "$base_image_name"
declare -i pull_status=$?
if [[ $pull_status -ne 0 ]]; then
echo "Docker pull exited with code $pull_status"
exit 1
fi
fi
if [[ -z "$image_name" ]]; then
image_name="${docker_user}:spark-dev"
fi
echo "Building image
image_name : $image_name
user : $docker_user"
if [[ -z "$docker_pass" ]]; then
docker_pass="$docker_user"
echo "Giving user '$docker_user' a secure password: $docker_pass"
fi
docker build \
--build-arg docker_user="$docker_user" \
--build-arg docker_pass="$docker_pass" \
--build-arg docker_uid="$docker_uid" \
-t "$image_name" \
.