surshar / claude-code
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
Built by Anthropic · github.com/anthropics
| @@ -1,0 +1,139 @@ | ||
| 1 | +#------------------------------------------------------------------------------ | |
| 2 | +# Script: run_devcontainer_claude_code.ps1 | |
| 3 | +# Author: [Your Name or Project Name] | |
| 4 | +# Description: Automates the setup and connection to a DevContainer environment | |
| 5 | +# using either Docker or Podman on Windows. | |
| 6 | +# | |
| 7 | +# IMPORTANT USAGE REQUIREMENT: | |
| 8 | +# This script MUST be executed from the ROOT directory of your project. | |
| 9 | +# It assumes the script file itself is located in a 'Script' subdirectory. | |
| 10 | +# | |
| 11 | +# Assumed Project Structure: | |
| 12 | +# Project/ | |
| 13 | +# ├── .devcontainer/ | |
| 14 | +# └── Script/ | |
| 15 | +# └── run_devcontainer_claude_code.ps1 <-- This script's location | |
| 16 | +# | |
| 17 | +# How to Run: | |
| 18 | +# 1. Open PowerShell. | |
| 19 | +# 2. Change your current directory to the project root: | |
| 20 | +# cd c:\path\to\your\Project | |
| 21 | +# 3. Execute the script, specifying the container backend: | |
| 22 | +# .\Script\run_devcontainer_claude_code.ps1 -Backend <docker|podman> | |
| 23 | +# | |
| 24 | +# The -Backend parameter is mandatory and accepts 'docker' or 'podman'. | |
| 25 | +#------------------------------------------------------------------------------ | |
| 26 | +[CmdletBinding()] | |
| 27 | +param( | |
| 28 | + [Parameter(Mandatory=$true)] | |
| 29 | + [string]$Backend | |
| 30 | +) | |
| 31 | + | |
| 32 | +# Notify script start | |
| 33 | +Write-Host "--- DevContainer Startup & Connection Script ---" | |
| 34 | +Write-Host "Using backend: $($Backend)" | |
| 35 | + | |
| 36 | +# Validate the input backend | |
| 37 | +if ($Backend -notin @('docker', 'podman')) { | |
| 38 | + Write-Error "Invalid backend specified. Please use 'docker' or 'podman'." | |
| 39 | + Write-Host "Usage: ." + $MyInvocation.MyCommand.Definition + " -Backend <docker|podman>" | |
| 40 | + exit 1 | |
| 41 | +} | |
| 42 | + | |
| 43 | +# --- Backend-Specific Initialization --- | |
| 44 | +if ($Backend -eq 'podman') { | |
| 45 | + Write-Host "--- Podman Backend Initialization ---" | |
| 46 | + | |
| 47 | + # --- Step 1a: Initialize Podman machine --- | |
| 48 | + Write-Host "Initializing Podman machine 'claudeVM'..." | |
| 49 | + try { | |
| 50 | + podman machine init claudeVM | |
| 51 | + Write-Host "Podman machine 'claudeVM' initialized or already exists." | |
| 52 | + } catch { | |
| 53 | + Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)" | |
| 54 | + exit 1 # Exit script on error | |
| 55 | + } | |
| 56 | + | |
| 57 | + # --- Step 1b: Start Podman machine --- | |
| 58 | + Write-Host "Starting Podman machine 'claudeVM'..." | |
| 59 | + try { | |
| 60 | + podman machine start claudeVM -q | |
| 61 | + Write-Host "Podman machine started or already running." | |
| 62 | + } catch { | |
| 63 | + Write-Error "Failed to start Podman machine: $($_.Exception.Message)" | |
| 64 | + exit 1 | |
| 65 | + } | |
| 66 | + | |
| 67 | + # --- Step 2: Set default connection --- | |
| 68 | + Write-Host "Setting default Podman connection to 'claudeVM'..." | |
| 69 | + try { | |
| 70 | + podman system connection default claudeVM | |
| 71 | + Write-Host "Default connection set." | |
| 72 | + } catch { | |
| 73 | + Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)" | |
| 74 | + } | |
| 75 | + | |
| 76 | +} elseif ($Backend -eq 'docker') { | |
| 77 | + Write-Host "--- Docker Backend Initialization ---" | |
| 78 | + | |
| 79 | + # --- Step 1 & 2: Check Docker Desktop --- | |
| 80 | + Write-Host "Checking if Docker Desktop is running and docker command is available..." | |
| 81 | + try { | |
| 82 | + docker info | Out-Null | |
| 83 | + Write-Host "Docker Desktop (daemon) is running." | |
| 84 | + } catch { | |
| 85 | + Write-Error "Docker Desktop is not running or docker command not found." | |
| 86 | + Write-Error "Please ensure Docker Desktop is running." | |
| 87 | + exit 1 | |
| 88 | + } | |
| 89 | +} | |
| 90 | + | |
| 91 | +# --- Step 3: Bring up DevContainer --- | |
| 92 | +Write-Host "Bringing up DevContainer in the current folder..." | |
| 93 | +try { | |
| 94 | + $devcontainerUpCommand = "devcontainer up --workspace-folder ." | |
| 95 | + if ($Backend -eq 'podman') { | |
| 96 | + $devcontainerUpCommand += " --docker-path podman" | |
| 97 | + } | |
| 98 | + Invoke-Expression $devcontainerUpCommand | |
| 99 | + Write-Host "DevContainer startup process completed." | |
| 100 | +} catch { | |
| 101 | + Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)" | |
| 102 | + exit 1 | |
| 103 | +} | |
| 104 | + | |
| 105 | +# --- Step 4: Get DevContainer ID --- | |
| 106 | +Write-Host "Finding the DevContainer ID..." | |
| 107 | +$currentFolder = (Get-Location).Path | |
| 108 | + | |
| 109 | +$psCommand = "$($Backend) ps --filter ""label=devcontainer.local_folder=$currentFolder"" --format ""{{.ID}}""" | |
| 110 | + | |
| 111 | +try { | |
| 112 | + $containerId = $(Invoke-Expression $psCommand).Trim() | |
| 113 | +} catch { | |
| 114 | + Write-Error "Failed to get container ID (Command: $psCommand): $($_.Exception.Message)" | |
| 115 | + exit 1 | |
| 116 | +} | |
| 117 | + | |
| 118 | +if (-not $containerId) { | |
| 119 | + Write-Error "Could not find DevContainer ID for the current folder ('$currentFolder')." | |
| 120 | + Write-Error "Please check if 'devcontainer up' was successful and the container is running." | |
| 121 | + exit 1 | |
| 122 | +} | |
| 123 | +Write-Host "Found container ID: $containerId" | |
| 124 | + | |
| 125 | +# --- Step 5 & 6: Execute command and enter interactive shell inside container --- | |
| 126 | +Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..." | |
| 127 | +try { | |
| 128 | + $execCommand = "$($Backend) exec -it $containerId zsh -c 'claude; exec zsh'" | |
| 129 | + Invoke-Expression $execCommand | |
| 130 | + | |
| 131 | + Write-Host "Interactive session ended." | |
| 132 | + | |
| 133 | +} catch { | |
| 134 | + Write-Error "Failed to execute command inside container (Command: $execCommand): $($_.Exception.Message)" | |
| 135 | + exit 1 | |
| 136 | +} | |
| 137 | + | |
| 138 | +# Notify script completion | |
| 139 | +Write-Host "--- Script completed ---" |