Skip to content

Instantly share code, notes, and snippets.

@weeping-somnambulist
Last active December 11, 2020 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weeping-somnambulist/3385ce6abc6394337446788fc2c5c227 to your computer and use it in GitHub Desktop.
Save weeping-somnambulist/3385ce6abc6394337446788fc2c5c227 to your computer and use it in GitHub Desktop.
Simple BASH script to start a vscode container
#!/usr/bin/env bash
set -o errexit
set -o pipefail
# Enable interruption signal handling
trap - INT TERM
# For debug mode change this to "TRUE"
DEBUG="FALSE"
# Global Variables
CURRENT_PATHNAME=$(basename $PWD)
VSCODE_CONFIG_PATH="$HOME/.local/share/lang_envs/rust/vscode/config"
VSCODE_EXTENSIONS_PATH="$HOME/.local/share/lang_envs/rust/vscode/extensions"
IMAGE_NAME="rust"
IMAGE_URL="app/vscode"
IMAGE_TAG="latest"
CONTAINER_HOME_PATH="$HOME"
# Test for path arguments
if [[ -n $1 ]]; then
if [[ "$1" == "." ]]; then
cd "$(pwd)";
else
cd $1;
fi;
else
cd "$(pwd)"
fi;
# IMMUTABLE CONTAINER SETTINGS
RUN_OPTIONS=(
--rm
--detach
--name "$IMAGE_NAME"-"$CURRENT_PATHNAME"
--hostname "$HOSTNAME"
--userns "keep-id"
--workdir "$CONTAINER_HOME_PATH"/"$CURRENT_PATHNAME"
--env DISPLAY="$DISPLAY"
--env XAUTHORITY="$XAUTHORITY"
--env QT_X11_NO_MITSHM="1"
--device /dev/snd:/dev/snd
--device /dev/dri:/dev/dri
--ipc "host"
)
# IMMUTABLE CONTAINER VOLUME MOUNTS
VOLUME_PATHS+=(
"$HOME"/.local/share/lang_envs/rust/shell:"$CONTAINER_HOME_PATH":rw
"$HOME"/.themes:"$CONTAINER_HOME_PATH"/.themes:ro
"$HOME"/.local/share/fonts:"$CONTAINER_HOME_PATH"/.local/share/fonts:ro
/tmp/.X11-unix:/tmp/.X11-unix:rw
"$XAUTHORITY":"$XAUTHORITY":rw
"$PWD":"$CONTAINER_HOME_PATH"/"$CURRENT_PATHNAME":rw
"$VSCODE_CONFIG_PATH":"$CONTAINER_HOME_PATH"/.config/Code:rw
"$VSCODE_EXTENSIONS_PATH":"$CONTAINER_HOME_PATH"/.vscode:rw
)
# Create our volume configuration strings
for i in ${VOLUME_PATHS[@]}; do
RUN_OPTIONS+=("--volume $i");
done
# Build the runtime string
RUNTIME="podman run ${RUN_OPTIONS[@]} $IMAGE_URL/$IMAGE_NAME:$IMAGE_TAG $@"
# Run our container...unless we're in debugging mode, then we just print the
# run string
if [[ $DEBUG = 'TRUE' ]]; then
echo $RUNTIME;
else
eval $RUNTIME > /dev/null;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment