spacehub

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

claude-code / Script / run_devcontainer_claude_code.ps1
ant-kurtMerge branch 'main' into main 944eb4e · on Jul 28, 2025 History
148 lines · 5.0 KBRaw
1<#
2.SYNOPSIS
3 Automates the setup and connection to a DevContainer environment using either Docker or Podman on Windows.
4
5.DESCRIPTION
6 This script automates the process of initializing, starting, and connecting to a DevContainer
7 using either Docker or Podman as the container backend. It must be executed from the root
8 directory of your project and assumes the script is located in a 'Script' subdirectory.
9
10.PARAMETER Backend
11 Specifies the container backend to use. Valid values are 'docker' or 'podman'.
12
13.EXAMPLE
14 .\Script\run_devcontainer_claude_code.ps1 -Backend docker
15 Uses Docker as the container backend.
16
17.EXAMPLE
18 .\Script\run_devcontainer_claude_code.ps1 -Backend podman
19 Uses Podman as the container backend.
20
21.NOTES
22 Project Structure:
23 Project/
24 ├── .devcontainer/
25 └── Script/
26 └── run_devcontainer_claude_code.ps1
27#>
28
29[CmdletBinding()]
30param(
31 [Parameter(Mandatory=$true)]
32 [ValidateSet('docker','podman')]
33 [string]$Backend
34)
35
36# Notify script start
37Write-Host "--- DevContainer Startup & Connection Script ---"
38Write-Host "Using backend: $($Backend)"
39
40# --- Prerequisite Check ---
41Write-Host "Checking for required commands..."
42try {
43 Get-Command $Backend -ErrorAction Stop | Out-Null
44 Write-Host "- $($Backend) command found."
45 Get-Command devcontainer -ErrorAction Stop | Out-Null
46 Write-Host "- devcontainer command found."
47}
48catch {
49 Write-Error "A required command is not installed or not in your PATH."
50 Write-Error "Please ensure '$($_.Exception.Message.Split(':')[0])' and 'devcontainer' are installed and accessible."
51 exit 1
52}
53
54
55# --- Backend-Specific Initialization ---
56if ($Backend -eq 'podman') {
57 Write-Host "--- Podman Backend Initialization ---"
58
59 # --- Step 1a: Initialize Podman machine ---
60 Write-Host "Initializing Podman machine 'claudeVM'..."
61 try {
62 & podman machine init claudeVM
63 Write-Host "Podman machine 'claudeVM' initialized or already exists."
64 } catch {
65 Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)"
66 exit 1 # Exit script on error
67 }
68
69 # --- Step 1b: Start Podman machine ---
70 Write-Host "Starting Podman machine 'claudeVM'..."
71 try {
72 & podman machine start claudeVM -q
73 Write-Host "Podman machine started or already running."
74 } catch {
75 Write-Error "Failed to start Podman machine: $($_.Exception.Message)"
76 exit 1
77 }
78
79 # --- Step 2: Set default connection ---
80 Write-Host "Setting default Podman connection to 'claudeVM'..."
81 try {
82 & podman system connection default claudeVM
83 Write-Host "Default connection set."
84 } catch {
85 Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)"
86 }
87
88} elseif ($Backend -eq 'docker') {
89 Write-Host "--- Docker Backend Initialization ---"
90
91 # --- Step 1 & 2: Check Docker Desktop ---
92 Write-Host "Checking if Docker Desktop is running and docker command is available..."
93 try {
94 docker info | Out-Null
95 Write-Host "Docker Desktop (daemon) is running."
96 } catch {
97 Write-Error "Docker Desktop is not running or docker command not found."
98 Write-Error "Please ensure Docker Desktop is running."
99 exit 1
100 }
101}
102
103# --- Step 3: Bring up DevContainer ---
104Write-Host "Bringing up DevContainer in the current folder..."
105try {
106 $arguments = @('up', '--workspace-folder', '.')
107 if ($Backend -eq 'podman') {
108 $arguments += '--docker-path', 'podman'
109 }
110 & devcontainer @arguments
111 Write-Host "DevContainer startup process completed."
112} catch {
113 Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)"
114 exit 1
115}
116
117# --- Step 4: Get DevContainer ID ---
118Write-Host "Finding the DevContainer ID..."
119$currentFolder = (Get-Location).Path
120
121try {
122 $containerId = (& $Backend ps --filter "label=devcontainer.local_folder=$currentFolder" --format '{{.ID}}').Trim()
123} catch {
124 $displayCommand = "$Backend ps --filter `"label=devcontainer.local_folder=$currentFolder`" --format '{{.ID}}'"
125 Write-Error "Failed to get container ID (Command: $displayCommand): $($_.Exception.Message)"
126 exit 1
127}
128
129if (-not $containerId) {
130 Write-Error "Could not find DevContainer ID for the current folder ('$currentFolder')."
131 Write-Error "Please check if 'devcontainer up' was successful and the container is running."
132 exit 1
133}
134Write-Host "Found container ID: $containerId"
135
136# --- Step 5 & 6: Execute command and enter interactive shell inside container ---
137Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..."
138try {
139 & $Backend exec -it $containerId zsh -c 'claude; exec zsh'
140 Write-Host "Interactive session ended."
141} catch {
142 $displayCommand = "$Backend exec -it $containerId zsh -c 'claude; exec zsh'"
143 Write-Error "Failed to execute command inside container (Command: $displayCommand): $($_.Exception.Message)"
144 exit 1
145}
146
147# Notify script completion
148Write-Host "--- Script completed ---"