From c586473a8b395c6ea8025e947de6f81496bb13f5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 26 Mar 2023 01:23:27 -0400 Subject: [PATCH] improvement: Expose ports for sparkd in docker container So the container can use virtual network adaptors, rather than shared the host's network stack. This allows us to run sparkd in a docker in WSL. --- start_docker.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/start_docker.sh b/start_docker.sh index b3268fb..a7246bf 100755 --- a/start_docker.sh +++ b/start_docker.sh @@ -15,6 +15,7 @@ Options: -i,--image The image to instantiate -c,--container The name for the container -m,--mount The name for the folder (on the host) that is mounted to container's 'home' dir + --port : or [:,...] Port mapping(s). May be specified multiple times. -h,--help Print this help message and exit EOF @@ -27,6 +28,7 @@ declare docker_pass="" declare image_name="" declare cont_name="" declare mount_name="" +declare -a port_mappings=() while [[ $# -gt 0 ]]; do case "$1" in @@ -76,6 +78,17 @@ while [[ $# -gt 0 ]]; do cont_name="$2" shift 2 ;; + --port) + # Parse port(s) arg, and append the result to 'port_mappings' + # Note: port(s) may be specified in bracketed notation: + # [a:b,c:d,...,y:z] + next_ports="$(echo "$2" | sed -e 's/\[//g' -e 's/\]//g' -e 's/,/ /g')" + if [[ "${#port_mappings[@]}" -gt 0 ]]; then + next_ports=" $next_ports" + fi + port_mappings+="$next_ports" + shift 2 + ;; -m) ;& --mount) @@ -144,6 +157,17 @@ fi mkdir -p $home_src +if [[ "${#port_mappings[@]}" -eq 0 ]]; then + port_mappings=("8000:8000" "8001:8001" "8002:8002" "8003:8003") +fi + +# Adapt port_mappings into args for `docker run` by prepending "-p " to each +# item +declare -a docker_port_args=() +for port_mapping in "${port_mappings[@]}"; do + docker_port_args+=("-p $port_mapping") +done + echo "Starting container '$cont_name' image_name : $image_name cont_name : $cont_name @@ -152,10 +176,13 @@ home_tgt : $home_tgt repo_src : $repo_src repo_tgt : $repo_tgt working dir : $working_dir +ports : ${port_mappings[*]} " +# DON'T wrap $docker_port_args in quotes. We want bash to split args by +# whitespace docker run \ - --net host \ + --net bridge \ -h "$host_name" \ -e "TZ=America/New_York" \ --cap-add=SYS_PTRACE \ @@ -164,6 +191,7 @@ docker run \ -v "$home_src:$home_tgt" \ -v "$repo_src:$repo_tgt" \ -w "$working_dir" \ + ${docker_port_args[*]} \ --shm-size=4024m \ -t \ -i \