Skip to content

Instantly share code, notes, and snippets.

@st0le
Last active April 8, 2026 19:18
Show Gist options
  • Select an option

  • Save st0le/564479a60f010136ec35b6d4f7704b50 to your computer and use it in GitHub Desktop.

Select an option

Save st0le/564479a60f010136ec35b6d4f7704b50 to your computer and use it in GitHub Desktop.
Container debugging
#!/bin/bash
# =============================================================================
# setup-container-debugging.sh
# =============================================================================
# Sets up .NET remote debugging tools (vsdbg) and devtunnel in containers
# for dev environments.
#
# Usage:
# ./setup-container-debugging.sh [--help]
#
# DO NOT USE IN PRODUCTION - This installs debug tools that should only exist
# in development environments.
# =============================================================================
set -e
# Configuration
VSDBG_INSTALL_PATH="/vsdbg"
DEVTUNNEL_INSTALL_PATH="/usr/local/bin/devtunnel"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
show_help() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Sets up .NET remote debugging tools (vsdbg and devtunnel) in a container.
Options:
--help Show this help message
Installs:
- vsdbg at: $VSDBG_INSTALL_PATH/vsdbg
- devtunnel at: $DEVTUNNEL_INSTALL_PATH
After setup, authenticate devtunnel with:
devtunnel user login --github
EOF
exit 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
;;
*)
print_error "Unknown option: $1"
show_help
;;
esac
done
# Detect package manager
detect_package_manager() {
if command -v tdnf &> /dev/null; then
echo "tdnf"
elif command -v apt-get &> /dev/null; then
echo "apt"
else
print_error "No supported package manager found (tdnf or apt-get)"
exit 1
fi
}
# Install dependencies based on package manager
install_dependencies() {
local pkg_manager=$1
print_info "Installing dependencies using $pkg_manager..."
if [ "$pkg_manager" = "tdnf" ]; then
tdnf install -y curl procps-ng socat gawk grep sed findutils tar gzip unzip 2>/dev/null || true
else
apt-get update -qq
apt-get install -y -qq curl procps socat gawk grep sed findutils tar gzip unzip net-tools 2>/dev/null || true
fi
}
# Install vsdbg debugger
install_vsdbg() {
if [ -f "$VSDBG_INSTALL_PATH/vsdbg" ]; then
print_info "vsdbg already installed at $VSDBG_INSTALL_PATH"
return 0
fi
print_info "Installing vsdbg debugger..."
# Use the official installer script
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l "$VSDBG_INSTALL_PATH"
if [ -f "$VSDBG_INSTALL_PATH/vsdbg" ]; then
print_info "vsdbg installed successfully at $VSDBG_INSTALL_PATH"
else
print_error "Failed to install vsdbg"
exit 1
fi
}
# Install devtunnel CLI (optional, for AKS without kubectl)
install_devtunnel() {
if [ -f "$DEVTUNNEL_INSTALL_PATH" ]; then
if file "$DEVTUNNEL_INSTALL_PATH" | grep -q "executable\|ELF"; then
print_info "devtunnel already installed at $DEVTUNNEL_INSTALL_PATH"
return 0
else
print_warn "Existing devtunnel appears corrupt, reinstalling..."
rm -f "$DEVTUNNEL_INSTALL_PATH"
fi
fi
print_info "Installing devtunnel CLI..."
curl -sL https://aka.ms/TunnelsCliDownload/linux-x64 -o /tmp/devtunnel
if file /tmp/devtunnel | grep -q "executable\|ELF"; then
chmod +x /tmp/devtunnel
mv /tmp/devtunnel "$DEVTUNNEL_INSTALL_PATH"
print_info "devtunnel installed successfully"
else
print_error "Failed to download devtunnel binary"
rm -f /tmp/devtunnel
exit 1
fi
}
# Print summary
print_summary() {
echo ""
echo "=============================================="
echo " Container Debug Setup Complete"
echo "=============================================="
echo ""
echo " vsdbg: $VSDBG_INSTALL_PATH/vsdbg"
echo " devtunnel: $DEVTUNNEL_INSTALL_PATH"
echo ""
echo " Next steps:"
echo ""
echo " 1. Authenticate devtunnel with GitHub:"
echo " devtunnel user login --github"
echo " (Enter code at github.com/login/device)"
echo ""
echo " 2. Connect to your dev machine's tunnel:"
echo " devtunnel connect <tunnel-id>"
echo ""
echo " 3. Start vsdbg on TCP port 4024 using socat:"
echo " socat TCP-LISTEN:4024,reuseaddr,fork EXEC:'/vsdbg/vsdbg --interpreter=vscode'"
echo ""
echo " 4. In VS Code on dev machine, use 'Attach via DevTunnel' config"
echo ""
echo " To find the dotnet process ID:"
echo " ps aux | grep dotnet"
echo ""
}
# Main
main() {
echo "=============================================="
echo " Container Debug Setup"
echo "=============================================="
echo ""
# Detect and install
local pkg_manager
pkg_manager=$(detect_package_manager)
print_info "Detected package manager: $pkg_manager"
install_dependencies "$pkg_manager"
install_vsdbg
install_devtunnel
print_summary
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment