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

Merge pull request #951 from Masa1984a/feat/Add-PowerShell-script-for-Windows-DevContainer-setupBrowse files
Add PowerShell script for Windows DevContainer setup (Docker/Podman)
ant-kurt authored on Jul 3, 2025 · GitHub committed on Jul 3, 2025 parents e16c985 + c93c724 · commit 0149827a77f6
1 changed file+133 -0compared with e16c985
AScript/run_devcontainer_claude_code.ps1
@@ -1,0 +1,133 @@
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()]
30+param(
31+ [Parameter(Mandatory=$true)]
32+ [ValidateSet('docker','podman')]
33+ [string]$Backend
34+)
35+
36+# Notify script start
37+Write-Host "--- DevContainer Startup & Connection Script ---"
38+Write-Host "Using backend: $($Backend)"
39+
40+# --- Backend-Specific Initialization ---
41+if ($Backend -eq 'podman') {
42+ Write-Host "--- Podman Backend Initialization ---"
43+
44+ # --- Step 1a: Initialize Podman machine ---
45+ Write-Host "Initializing Podman machine 'claudeVM'..."
46+ try {
47+ & podman machine init claudeVM
48+ Write-Host "Podman machine 'claudeVM' initialized or already exists."
49+ } catch {
50+ Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)"
51+ exit 1 # Exit script on error
52+ }
53+
54+ # --- Step 1b: Start Podman machine ---
55+ Write-Host "Starting Podman machine 'claudeVM'..."
56+ try {
57+ & podman machine start claudeVM -q
58+ Write-Host "Podman machine started or already running."
59+ } catch {
60+ Write-Error "Failed to start Podman machine: $($_.Exception.Message)"
61+ exit 1
62+ }
63+
64+ # --- Step 2: Set default connection ---
65+ Write-Host "Setting default Podman connection to 'claudeVM'..."
66+ try {
67+ & podman system connection default claudeVM
68+ Write-Host "Default connection set."
69+ } catch {
70+ Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)"
71+ }
72+
73+} elseif ($Backend -eq 'docker') {
74+ Write-Host "--- Docker Backend Initialization ---"
75+
76+ # --- Step 1 & 2: Check Docker Desktop ---
77+ Write-Host "Checking if Docker Desktop is running and docker command is available..."
78+ try {
79+ docker info | Out-Null
80+ Write-Host "Docker Desktop (daemon) is running."
81+ } catch {
82+ Write-Error "Docker Desktop is not running or docker command not found."
83+ Write-Error "Please ensure Docker Desktop is running."
84+ exit 1
85+ }
86+}
87+
88+# --- Step 3: Bring up DevContainer ---
89+Write-Host "Bringing up DevContainer in the current folder..."
90+try {
91+ $arguments = @('up', '--workspace-folder', '.')
92+ if ($Backend -eq 'podman') {
93+ $arguments += '--docker-path', 'podman'
94+ }
95+ & devcontainer @arguments
96+ Write-Host "DevContainer startup process completed."
97+} catch {
98+ Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)"
99+ exit 1
100+}
101+
102+# --- Step 4: Get DevContainer ID ---
103+Write-Host "Finding the DevContainer ID..."
104+$currentFolder = (Get-Location).Path
105+
106+try {
107+ $containerId = (& $Backend ps --filter "label=devcontainer.local_folder=$currentFolder" --format '{{.ID}}').Trim()
108+} catch {
109+ $displayCommand = "$Backend ps --filter `"label=devcontainer.local_folder=$currentFolder`" --format '{{.ID}}'"
110+ Write-Error "Failed to get container ID (Command: $displayCommand): $($_.Exception.Message)"
111+ exit 1
112+}
113+
114+if (-not $containerId) {
115+ Write-Error "Could not find DevContainer ID for the current folder ('$currentFolder')."
116+ Write-Error "Please check if 'devcontainer up' was successful and the container is running."
117+ exit 1
118+}
119+Write-Host "Found container ID: $containerId"
120+
121+# --- Step 5 & 6: Execute command and enter interactive shell inside container ---
122+Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..."
123+try {
124+ & $Backend exec -it $containerId zsh -c 'claude; exec zsh'
125+ Write-Host "Interactive session ended."
126+} catch {
127+ $displayCommand = "$Backend exec -it $containerId zsh -c 'claude; exec zsh'"
128+ Write-Error "Failed to execute command inside container (Command: $displayCommand): $($_.Exception.Message)"
129+ exit 1
130+}
131+
132+# Notify script completion
133+Write-Host "--- Script completed ---"