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 | <# |
| 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 | [CmdletBinding()] |
| 29 | param( |
| 30 | [Parameter(Mandatory=$true)] |
| 31 | [ValidateSet('docker','podman')] |
| 32 | [string]$Backend |
| 33 | ) |
| 34 | |
| 35 | # Notify script start |
| 36 | Write-Host "--- DevContainer Startup & Connection Script ---" |
| 37 | Write-Host "Using backend: $($Backend)" |
| 38 | |
| 39 | # --- Backend-Specific Initialization --- |
| 40 | if ($Backend -eq 'podman') { |
| 41 | Write-Host "--- Podman Backend Initialization ---" |
| 42 | |
| 43 | # --- Step 1a: Initialize Podman machine --- |
| 44 | Write-Host "Initializing Podman machine 'claudeVM'..." |
| 45 | try { |
| 46 | podman machine init claudeVM |
| 47 | Write-Host "Podman machine 'claudeVM' initialized or already exists." |
| 48 | } catch { |
| 49 | Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)" |
| 50 | exit 1 # Exit script on error |
| 51 | } |
| 52 | |
| 53 | # --- Step 1b: Start Podman machine --- |
| 54 | Write-Host "Starting Podman machine 'claudeVM'..." |
| 55 | try { |
| 56 | podman machine start claudeVM -q |
| 57 | Write-Host "Podman machine started or already running." |
| 58 | } catch { |
| 59 | Write-Error "Failed to start Podman machine: $($_.Exception.Message)" |
| 60 | exit 1 |
| 61 | } |
| 62 | |
| 63 | # --- Step 2: Set default connection --- |
| 64 | Write-Host "Setting default Podman connection to 'claudeVM'..." |
| 65 | try { |
| 66 | podman system connection default claudeVM |
| 67 | Write-Host "Default connection set." |
| 68 | } catch { |
| 69 | Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)" |
| 70 | } |
| 71 | |
| 72 | } elseif ($Backend -eq 'docker') { |
| 73 | Write-Host "--- Docker Backend Initialization ---" |
| 74 | |
| 75 | # --- Step 1 & 2: Check Docker Desktop --- |
| 76 | Write-Host "Checking if Docker Desktop is running and docker command is available..." |
| 77 | try { |
| 78 | docker info | Out-Null |
| 79 | Write-Host "Docker Desktop (daemon) is running." |
| 80 | } catch { |
| 81 | Write-Error "Docker Desktop is not running or docker command not found." |
| 82 | Write-Error "Please ensure Docker Desktop is running." |
| 83 | exit 1 |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | # --- Step 3: Bring up DevContainer --- |
| 88 | Write-Host "Bringing up DevContainer in the current folder..." |
| 89 | try { |
| 90 | $devcontainerUpCommand = "devcontainer up --workspace-folder ." |
| 91 | if ($Backend -eq 'podman') { |
| 92 | $devcontainerUpCommand += " --docker-path podman" |
| 93 | } |
| 94 | Invoke-Expression $devcontainerUpCommand |
| 95 | Write-Host "DevContainer startup process completed." |
| 96 | } catch { |
| 97 | Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)" |
| 98 | exit 1 |
| 99 | } |
| 100 | |
| 101 | # --- Step 4: Get DevContainer ID --- |
| 102 | Write-Host "Finding the DevContainer ID..." |
| 103 | $currentFolder = (Get-Location).Path |
| 104 | |
| 105 | $psCommand = "$($Backend) ps --filter ""label=devcontainer.local_folder=$currentFolder"" --format ""{{.ID}}""" |
| 106 | |
| 107 | try { |
| 108 | $containerId = $(Invoke-Expression $psCommand).Trim() |
| 109 | } catch { |
| 110 | Write-Error "Failed to get container ID (Command: $psCommand): $($_.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 | $execCommand = "$($Backend) exec -it $containerId zsh -c 'claude; exec zsh'" |
| 125 | Invoke-Expression $execCommand |
| 126 | |
| 127 | Write-Host "Interactive session ended." |
| 128 | |
| 129 | } catch { |
| 130 | Write-Error "Failed to execute command inside container (Command: $execCommand): $($_.Exception.Message)" |
| 131 | exit 1 |
| 132 | } |
| 133 | |
| 134 | # Notify script completion |
| 135 | Write-Host "--- Script completed ---" |