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
152 lines · 5.2 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 if (-not (Get-Command $Backend -ErrorAction SilentlyContinue)) {
44 throw "Required command '$($Backend)' not found."
45 }
46 Write-Host "- $($Backend) command found."
47 if (-not (Get-Command devcontainer -ErrorAction SilentlyContinue)) {
48 throw "Required command 'devcontainer' not found."
49 }
50 Write-Host "- devcontainer command found."
51}
52catch {
53 Write-Error "A required command is not installed or not in your PATH. $($_.Exception.Message)"
54 Write-Error "Please ensure both '$Backend' and 'devcontainer' are installed and accessible in your system's PATH."
55 exit 1
56}
57
58
59# --- Backend-Specific Initialization ---
60if ($Backend -eq 'podman') {
61 Write-Host "--- Podman Backend Initialization ---"
62
63 # --- Step 1a: Initialize Podman machine ---
64 Write-Host "Initializing Podman machine 'claudeVM'..."
65 try {
66 & podman machine init claudeVM
67 Write-Host "Podman machine 'claudeVM' initialized or already exists."
68 } catch {
69 Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)"
70 exit 1 # Exit script on error
71 }
72
73 # --- Step 1b: Start Podman machine ---
74 Write-Host "Starting Podman machine 'claudeVM'..."
75 try {
76 & podman machine start claudeVM -q
77 Write-Host "Podman machine started or already running."
78 } catch {
79 Write-Error "Failed to start Podman machine: $($_.Exception.Message)"
80 exit 1
81 }
82
83 # --- Step 2: Set default connection ---
84 Write-Host "Setting default Podman connection to 'claudeVM'..."
85 try {
86 & podman system connection default claudeVM
87 Write-Host "Default connection set."
88 } catch {
89 Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)"
90 }
91
92} elseif ($Backend -eq 'docker') {
93 Write-Host "--- Docker Backend Initialization ---"
94
95 # --- Step 1 & 2: Check Docker Desktop ---
96 Write-Host "Checking if Docker Desktop is running and docker command is available..."
97 try {
98 docker info | Out-Null
99 Write-Host "Docker Desktop (daemon) is running."
100 } catch {
101 Write-Error "Docker Desktop is not running or docker command not found."
102 Write-Error "Please ensure Docker Desktop is running."
103 exit 1
104 }
105}
106
107# --- Step 3: Bring up DevContainer ---
108Write-Host "Bringing up DevContainer in the current folder..."
109try {
110 $arguments = @('up', '--workspace-folder', '.')
111 if ($Backend -eq 'podman') {
112 $arguments += '--docker-path', 'podman'
113 }
114 & devcontainer @arguments
115 Write-Host "DevContainer startup process completed."
116} catch {
117 Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)"
118 exit 1
119}
120
121# --- Step 4: Get DevContainer ID ---
122Write-Host "Finding the DevContainer ID..."
123$currentFolder = (Get-Location).Path
124
125try {
126 $containerId = (& $Backend ps --filter "label=devcontainer.local_folder=$currentFolder" --format '{{.ID}}').Trim()
127} catch {
128 $displayCommand = "$Backend ps --filter `"label=devcontainer.local_folder=$currentFolder`" --format '{{.ID}}'"
129 Write-Error "Failed to get container ID (Command: $displayCommand): $($_.Exception.Message)"
130 exit 1
131}
132
133if (-not $containerId) {
134 Write-Error "Could not find DevContainer ID for the current folder ('$currentFolder')."
135 Write-Error "Please check if 'devcontainer up' was successful and the container is running."
136 exit 1
137}
138Write-Host "Found container ID: $containerId"
139
140# --- Step 5 & 6: Execute command and enter interactive shell inside container ---
141Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..."
142try {
143 & $Backend exec -it $containerId zsh -c 'claude; exec zsh'
144 Write-Host "Interactive session ended."
145} catch {
146 $displayCommand = "$Backend exec -it $containerId zsh -c 'claude; exec zsh'"
147 Write-Error "Failed to execute command inside container (Command: $displayCommand): $($_.Exception.Message)"
148 exit 1
149}
150
151# Notify script completion
152Write-Host "--- Script completed ---"