From 199dc7553b2573c5576413a5c002b43df17e217a Mon Sep 17 00:00:00 2001 From: Ian Koerich Maciel Date: Tue, 5 Jul 2022 18:01:23 -0300 Subject: [PATCH 1/3] Add suport for .devcontainer.json file Devcontainer configuration might be saved on .devcontainer folder or .devcontainer.json. This commit add the missing support for .devcontainer.json file. --- devcontainer.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/devcontainer.sh b/devcontainer.sh index 101198e..99ef44f 100755 --- a/devcontainer.sh +++ b/devcontainer.sh @@ -28,8 +28,15 @@ debug "CONFIG_DIR: ${CONFIG_DIR}" CONFIG_FILE=devcontainer.json debug "CONFIG_FILE: ${CONFIG_FILE}" if ! [ -e "$CONFIG_DIR/$CONFIG_FILE" ]; then - echo "Folder contains no devcontainer configuration" - exit + # Config file may also be ".devcontainer.json" on the project folder. + CONFIG_DIR=. + debug "CONFIG_DIR: ${CONFIG_DIR}" + CONFIG_FILE=.devcontainer.json + debug "CONFIG_FILE: ${CONFIG_FILE}" + if ! [ -e "$CONFIG_DIR/$CONFIG_FILE" ]; then + echo "Folder contains no devcontainer configuration" + exit + fi fi CONFIG=$(cat $CONFIG_DIR/$CONFIG_FILE | grep -v //) From eafb8f17c7d62e3ed26517ea8d3f81388c664c0c Mon Sep 17 00:00:00 2001 From: Ian Koerich Maciel Date: Wed, 6 Jul 2022 16:43:32 -0300 Subject: [PATCH 2/3] Mount workspace on the same location as vscode vscode mounts the entire project (where .git is located) on /workspace. This commit changes the script behavior to find the .git folder, mount this bind and use the current workspace inside the docker just like vscode. --- devcontainer.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/devcontainer.sh b/devcontainer.sh index 99ef44f..1d147b0 100755 --- a/devcontainer.sh +++ b/devcontainer.sh @@ -19,9 +19,9 @@ debug() { fi } -WORKSPACE=${1:-`pwd`} +PROJECT_ROOT=$(git rev-parse --show-toplevel) CURRENT_DIR=${PWD##*/} -echo "Using workspace ${WORKSPACE}" +echo "Using workspace ${PROJECT_ROOT}" CONFIG_DIR=./.devcontainer debug "CONFIG_DIR: ${CONFIG_DIR}" @@ -73,11 +73,14 @@ debug "PORTS: ${PORTS}" ENVS=$(echo $CONFIG | jq -r '.remoteEnv | to_entries? | map("-e \(.key)=\(.value)")? | join(" ")') debug "ENVS: ${ENVS}" -WORK_DIR="/workspace" +TARGET_PROJECT_ROOT="/workspace/$(basename $PROJECT_ROOT)" debug "WORK_DIR: ${WORK_DIR}" -MOUNT="${MOUNT} --mount type=bind,source=${WORKSPACE},target=${WORK_DIR}" -debug "MOUNT: ${MOUNT}" +MOUNT="${MOUNT} --mount type=bind,source=${PROJECT_ROOT},target=${TARGET_PROJECT_ROOT}" +debug "MOUNT: ${TARGET_PROJECT_ROOT}" + +WORK_DIR=$(echo "$TARGET_PROJECT_ROOT${PWD#"$PROJECT_ROOT"}") +debug "WORK_DIR: ${WORK_DIR}" echo "Building and starting container" DOCKER_IMAGE_HASH=$(docker build -f $DOCKER_FILE $ARGS .) From 744379637428e634839933b4b44f7955e043239b Mon Sep 17 00:00:00 2001 From: Ian Koerich Maciel Date: Wed, 6 Jul 2022 16:46:33 -0300 Subject: [PATCH 3/3] Fix image missing image hash The output of a docker build command is not an image hash and this were causing error on docker run command. This commit parses the build command output to extract only the hash and use later to run the docker. --- devcontainer.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/devcontainer.sh b/devcontainer.sh index 1d147b0..dcb3cff 100755 --- a/devcontainer.sh +++ b/devcontainer.sh @@ -83,7 +83,8 @@ WORK_DIR=$(echo "$TARGET_PROJECT_ROOT${PWD#"$PROJECT_ROOT"}") debug "WORK_DIR: ${WORK_DIR}" echo "Building and starting container" -DOCKER_IMAGE_HASH=$(docker build -f $DOCKER_FILE $ARGS .) +DOCKER_BUILD_OUTPUT=$(docker build -f $DOCKER_FILE $ARGS .) +DOCKER_IMAGE_HASH=$(echo $DOCKER_BUILD_OUTPUT | awk '/Successfully built/{print $NF}') debug "DOCKER_IMAGE_HASH: ${DOCKER_IMAGE_HASH}" docker run -it $REMOTE_USER $PORTS $ENVS $MOUNT -w $WORK_DIR $DOCKER_IMAGE_HASH $SHELL \ No newline at end of file