feat: Add dev Dockerfile & scripts to integrate w/ devcontainers

This commit is contained in:
John Colvin committed 2023-03-31 04:59:33 +00:00
1 parent 7f5fc417c2
commit 81567b1f82
4 files changed
+253

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
FROM johncolvin/bluetradingsystems:spark-dev
ARG docker_user
ARG docker_uid
ARG docker_pass
RUN \
groupadd -g "$docker_uid" "$docker_user" \
&& useradd -u "$docker_uid" -g "$docker_uid" "$docker_user" \
&& usermod --password "$(openssl passwd -1 "$docker_pass")" "$docker_user" \
&& usermod -aG sudo "$docker_user"
# Set the default user
USER $docker_user:$docker_user
+40
View File
@@ -0,0 +1,40 @@
# Spark Dev Container
## How to create a Spark VS Code Devcontainer
<a name="build_image"></a>
## 1. Build the Docker image
Run the script `build.sh` located in this directory to build the image
```
build.sh
```
***Notes***
* Builds an image named `$USER:spark-dev`
* Adds a user to the image with the same name/uid/gid as the current user so
it has proper file permissions in mounted directories
* Run `build.sh --help` to see available options
<a name="gen_devcontainer_json"></a>
## 2. Generate a devcontainer.json
Run the script `gen_devcontainer.sh` located in this directory to create a devcontainer.json file.<br>
***Note:** The file path must be `<repo_dir>/.devcontainer/devcontainer.json`*
```
# Dump `gen_devcontainer.sh` output to devcontainer.json
gen_devcontainer.sh > $workspaceFolder/.devcontainer/devcontainer.json
```
***Notes***
* Mounts the following directories (`<container_path>:<host_path>`):
- `/home/<user>:/home/<user>/.spark-dev/mounts/<container_name>`
- `/home/<user>/spark:/path/to/this/repo`
* Run `gen_devcontainer.sh --help` to see available options
<a name="open_vscode_in_container"></a>
## 3. Open VS Code in Container
1. Launch **VS Code** and open the repo folder on the host machine.
1. Run **Dev Containers: Open Folder in Container** from the command palette.<br>
***Notes**
* The container stops when you close VS Code. All processes, including
`sparkd`, will be suspened until you open the workspace again (or manually
restart the container).
Executable
+123
View File
@@ -0,0 +1,123 @@
#!/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" \
.
+76
View File
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
function print_help_and_exit() {
cat <<EOF
Generate a devcontainer json object for the current workspace.
Usage: gen_devcontainer_json.sh [OPTIONS]
Options:
-u,--user USER The user to add to the image (defaults to the current user)
-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 image_name=""
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
;;
-h)
;&
--help)
print_help_and_exit 0
;;
*)
echo "Unexpected argument '$1'"
print_help_and_exit 1
;;
esac
done
if [[ -z "$docker_user" ]]; then
docker_user="$USER"
fi
if [[ -z "$image_name" ]]; then
image_name="${docker_user}:spark-dev"
fi
cat <<EOF
{
"image": "$image_name",
"remoteUser": "$docker_user",
"containerUser": "$docker_user",
"initializeCommand": "mkdir -p $HOME/.spark-dev/mounts/spark-dev",
"workspaceMount": "source=\${localWorkspaceFolder},target=/home/$docker_user/spark,type=bind",
"workspaceFolder": "/home/$docker_user/spark",
"mounts": [
"source=$HOME/.spark-dev/mounts/spark-dev,target=/home/$docker_user,type=bind"
],
}
EOF