Created
February 28, 2025 10:46
-
-
Save Joshuaalbert/a3ab5812071a5c611ba98ff728f9f55c to your computer and use it in GitHub Desktop.
Nice setup for bash for my workflows. Put `source setup_shell.sh` into `.bashrc`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias feh='feh --keep-zoom-vp' | |
alias dus='du -s -BM * | sort -n' | |
alias carta='docker run -ti -p 3002:3002 -v $PWD/.carta:/home/cartauser/.carta -v $PWD:/images cartavis/carta --no_log' | |
docker-shell() { | |
# Help message | |
if [[ "$1" == "--help" ]]; then | |
echo "Usage: docker-shell [user|root] [image_name] [cpu|gpu]" | |
echo "Default: docker-shell user debian:buster-slim cpu" | |
echo "Example: docker-shell root ubuntu:latest gpu" | |
return 0 | |
fi | |
# Input arguments | |
USER_MODE="${1:-user}" # 'user' or 'root' | |
IMAGE_NAME="${2:-debian:buster-slim}" | |
GPU_MODE="${3:-cpu}" # 'gpu' to enable GPU, anything else or empty for CPU only | |
# User details | |
USER_ID=$(id -u) | |
GROUP_ID=$(id -g) | |
USER_NAME=$(id -un) | |
USER_HOME=$HOME | |
# Container settings | |
# CONTAINER_NAME="$(basename "$(pwd)")" | |
DOCKER_CACHE_DIR="./.docker_cache" | |
DOCKER_LOCAL_DIR="./.docker_local" | |
# Create necessary directories | |
mkdir -p "${DOCKER_CACHE_DIR}" | |
mkdir -p "${DOCKER_LOCAL_DIR}" | |
echo "Creating/Using cache directory at: ${DOCKER_CACHE_DIR}" | |
echo "Creating/Using local directory at: ${DOCKER_LOCAL_DIR}" | |
# Determine if running as root or host user | |
if [ "$USER_MODE" = "root" ]; then | |
CONTAINER_USER="root" | |
CONTAINER_HOME="/root" | |
SSH_MOUNTS=() | |
echo "⚠️ Not mounting SSH keys or Git config in root mode for security reasons." | |
else | |
CONTAINER_USER="${USER_ID}:${GROUP_ID}" | |
CONTAINER_HOME="/home/${USER_NAME}" | |
# Only mount SSH and Git config for non-root mode | |
SSH_MOUNTS=(-v "${USER_HOME}/.ssh:${CONTAINER_HOME}/.ssh:ro" -v "${USER_HOME}/.gitconfig:${CONTAINER_HOME}/.gitconfig:ro") | |
fi | |
CACHE_DIR="${CONTAINER_HOME}/.cache" | |
LOCAL_DIR="${CONTAINER_HOME}/.local" | |
# Ensure the Docker image is present | |
docker image inspect "$IMAGE_NAME" >/dev/null 2>&1 || docker pull "$IMAGE_NAME" | |
# Define environment variables as an array | |
ENV_VARS=( | |
-e USER="${USER_NAME}" | |
-e HOME="${CONTAINER_HOME}" | |
) | |
# Only add UID and GID in user mode | |
if [ "$USER_MODE" != "root" ]; then | |
ENV_VARS+=(-e UID="${USER_ID}" -e GID="${GROUP_ID}") | |
fi | |
# Check if GPU support is needed | |
if [ "$GPU_MODE" = "gpu" ]; then | |
GPU_OPTION="--gpus all" | |
ENV_VARS+=( | |
-e NVIDIA_VISIBLE_DEVICES=all | |
-e NVIDIA_DRIVER_CAPABILITIES=compute,utility | |
) | |
echo "🚀 GPU support enabled!" | |
else | |
GPU_OPTION="" | |
echo "⚙️ Running in CPU mode." | |
fi | |
docker run -it --rm \ | |
--init \ | |
--entrypoint bash \ | |
--pid=host \ | |
-u "${CONTAINER_USER}" \ | |
-v "$(pwd):/workspace:delegated" \ | |
-v "${DOCKER_CACHE_DIR}:${CACHE_DIR}" \ | |
-v "${DOCKER_LOCAL_DIR}:${LOCAL_DIR}" \ | |
-v /etc/passwd:/etc/passwd:ro \ | |
-v /etc/group:/etc/group:ro \ | |
"${SSH_MOUNTS[@]}" \ | |
--group-add "${GROUP_ID}" \ | |
"${ENV_VARS[@]}" \ | |
$GPU_OPTION \ | |
-w /workspace \ | |
"$IMAGE_NAME" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment