Skip to content

Commit 0c96d6f

Browse files
Add Dev Container for Codespaces (#44)
1 parent bc53880 commit 0c96d6f

17 files changed

+966
-54
lines changed

.devcontainer/README.md

Lines changed: 113 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,69 @@ All packages from `requirements.txt` are pre-installed:
4747
### Environment Configuration
4848
- **PYTHONPATH** - Automatically configured to include shared Python modules
4949
- **Jupyter Kernel** - Custom kernel named "APIM Samples Python"
50-
- **Azure CLI** - Configured for container-friendly authentication
50+
- **Azure CLI** - Installed and ready for authentication (requires tenant-specific `az login` inside container)
5151
- **Port Forwarding** - Common development ports (3000, 5000, 8000, 8080) pre-configured
5252

5353
## 🔧 Post-Setup Steps
5454

55-
After the container starts, you'll need to:
55+
The dev container automatically handles most setup during initialization. During the first build, you'll be prompted to configure Azure CLI authentication.
5656

57-
1. **Sign in to Azure**:
58-
```bash
59-
az login
60-
```
57+
### Automated Interactive Setup (During First Build)
58+
When the container starts for the first time, the setup script will automatically:
59+
1. **Install all dependencies** (Python packages, Azure CLI extensions)
60+
2. **Configure Jupyter environment** with custom kernel
61+
3. **Prompt for Azure CLI configuration**:
62+
- Mount local Azure config (preserves login between rebuilds)
63+
- Use manual login (run tenant-specific `az login` each time)
64+
- Configure manually later
65+
66+
### Manual Configuration (If Needed Later)
67+
If you skipped the initial configuration or want to change it:
68+
69+
**Interactive Setup**:
70+
```bash
71+
python .devcontainer/configure-azure-mount.py
72+
```
73+
74+
**Manual Azure Login**:
75+
```bash
76+
# Log in to your specific tenant
77+
az login --tenant <your-tenant-id-or-domain>
6178

62-
2. **Verify your Azure setup**:
63-
Execute `shared/jupyter/verify-az-account.ipynb`
79+
# Set your target subscription
80+
az account set --subscription <your-subscription-id-or-name>
81+
82+
# Verify your authentication context
83+
az account show
84+
```
6485

86+
### Continue with Development
87+
After setup is complete:
88+
1. **Verify your Azure setup**: Execute `shared/jupyter/verify-az-account.ipynb`
89+
2. **Test your environment**: Run `python .devcontainer/verify-setup.py`
6590
3. **Start exploring**:
6691
- Navigate to any infrastructure folder (`infrastructure/`)
6792
- Run the `create.ipynb` notebook to set up infrastructure
6893
- Explore samples in the `samples/` directory
6994

95+
## 🔧 Troubleshooting
96+
97+
If you encounter import errors or module resolution issues, see:
98+
- [Import Troubleshooting Guide](.devcontainer/IMPORT-TROUBLESHOOTING.md)
99+
- [Setup Notes](.devcontainer/SETUP-NOTES.md)
100+
101+
Common quick fixes:
102+
```bash
103+
# Fix Python path for local development
104+
python setup/setup_python_path.py --generate-env
105+
106+
# Verify setup
107+
python .devcontainer/verify-setup.py
108+
109+
# Rebuild dev container if needed
110+
Dev Containers: Rebuild Container
111+
```
112+
70113
## 🏗️ Architecture
71114

72115
The dev container is built on:
@@ -77,10 +120,67 @@ The dev container is built on:
77120

78121
## 🔄 Azure CLI Authentication
79122

80-
The container mounts your local `~/.azure` directory to preserve authentication state between container rebuilds. This means:
81-
- Your Azure login persists across sessions
82-
- Your Azure CLI configuration is maintained
83-
- No need to repeatedly authenticate
123+
### Quick Setup (Recommended)
124+
125+
Run the interactive configuration script to automatically set up Azure CLI authentication for your platform:
126+
127+
**Python (Cross-platform)**:
128+
```bash
129+
python .devcontainer/configure-azure-mount.py
130+
```
131+
132+
**PowerShell (Windows)**:
133+
```powershell
134+
.\.devcontainer\configure-azure-mount.ps1
135+
```
136+
137+
**Bash (Linux/macOS)**:
138+
```bash
139+
./.devcontainer/configure-azure-mount.sh
140+
```
141+
142+
### Configuration Options
143+
144+
The setup script provides three choices:
145+
146+
**Option 1: Mount local Azure CLI config**
147+
- ✅ Preserves login between container rebuilds
148+
- ✅ Uses your existing tenant-specific `az login` from host machine
149+
- ✅ Works on Windows (`${localEnv:USERPROFILE}/.azure`) and Unix (`${localEnv:HOME}/.azure`)
150+
- ✅ Best for: Personal development with stable logins
151+
152+
**Option 2: Use manual login inside container [RECOMMENDED]**
153+
- ✅ Run tenant-specific `az login` each time container starts
154+
- ✅ More secure, fresh authentication each session
155+
- ✅ Works universally across all platforms and environments
156+
- ✅ Best for: Shared environments, GitHub Codespaces
157+
- ✅ Ensures you're working with the correct tenant and subscription
158+
159+
**Option 3: Configure manually later**
160+
- ✅ No changes made to devcontainer.json
161+
- ✅ You can edit the configuration files yourself
162+
- ✅ Full control over mount configuration
163+
164+
### Mount Preservation
165+
166+
The configuration script intelligently preserves any existing mounts (like SSH keys, additional volumes) while only managing Azure CLI mounts. This ensures your custom development setup remains intact.
167+
168+
### Non-Interactive Environments
169+
170+
In environments like GitHub Codespaces automation, the script automatically detects non-interactive contexts and safely defaults to Option 2 (manual login) for maximum reliability.
171+
172+
### Manual Options
173+
174+
**Option 1: Mount Local Azure Config**
175+
- Preserves authentication between container rebuilds
176+
- Platform-specific (configured automatically by the setup script)
177+
178+
**Option 2: Manual Login**
179+
- Log in to your specific tenant: `az login --tenant <your-tenant-id-or-domain>`
180+
- Set your target subscription: `az account set --subscription <your-subscription-id-or-name>`
181+
- Verify context: `az account show`
182+
- Works universally across all platforms
183+
- Requires re-authentication after container rebuilds
84184

85185
## 🐛 Troubleshooting
86186

@@ -119,7 +219,7 @@ If you need to rebuild the container:
119219

120220
## 🔒 Security Considerations
121221

122-
- Azure credentials are mounted from your local machine
222+
- Azure credentials are handled through tenant-specific `az login` inside the container (or optionally mounted)
123223
- The container runs as a non-root user (`vscode`)
124224
- All dependencies are installed from official sources
125225
- Network access is controlled through VS Code's port forwarding
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Azure CLI Configuration Setup for APIM Samples Dev Container
2+
# This PowerShell script provides an alternative to the Python configuration script
3+
4+
Write-Host "🚀 APIM Samples Dev Container Azure CLI Setup" -ForegroundColor Cyan
5+
Write-Host "==================================================" -ForegroundColor Cyan
6+
7+
Write-Host "🔧 Azure CLI Configuration Setup" -ForegroundColor Yellow
8+
Write-Host "========================================" -ForegroundColor Yellow
9+
10+
# Detect platform
11+
$platform = "Windows"
12+
Write-Host "Detected platform: $platform" -ForegroundColor Green
13+
14+
Write-Host "`nHow would you like to handle Azure CLI authentication?" -ForegroundColor White
15+
Write-Host "1. Mount local Azure CLI config (preserves login between container rebuilds)" -ForegroundColor White
16+
Write-Host "2. Use manual tenant-specific login inside container (requires explicit tenant/subscription setup each time)" -ForegroundColor White
17+
Write-Host "3. Let me configure this manually later" -ForegroundColor White
18+
19+
do {
20+
$choice = Read-Host "`nEnter your choice (1-3)"
21+
} while ($choice -notin @("1", "2", "3"))
22+
23+
# Path to devcontainer.json
24+
$devcontainerPath = Join-Path $PSScriptRoot "devcontainer.json"
25+
$backupPath = "$devcontainerPath.backup"
26+
27+
if (-not (Test-Path $devcontainerPath)) {
28+
Write-Host "❌ devcontainer.json not found at: $devcontainerPath" -ForegroundColor Red
29+
exit 1
30+
}
31+
32+
# Create backup
33+
try {
34+
Copy-Item $devcontainerPath $backupPath -Force
35+
Write-Host "✅ Backup created: $backupPath" -ForegroundColor Green
36+
} catch {
37+
Write-Host "❌ Failed to create backup: $_" -ForegroundColor Red
38+
exit 1
39+
}
40+
41+
# Read and parse devcontainer.json (simple approach for PowerShell)
42+
try {
43+
$content = Get-Content $devcontainerPath -Raw | ConvertFrom-Json -Depth 10
44+
45+
# Remove existing mounts if present
46+
if ($content.PSObject.Properties.Name -contains "mounts") {
47+
$content.PSObject.Properties.Remove("mounts")
48+
}
49+
50+
switch ($choice) {
51+
"1" {
52+
# Add Windows mount configuration
53+
$mount = @{
54+
source = "`${localEnv:USERPROFILE}/.azure"
55+
target = "/home/vscode/.azure"
56+
type = "bind"
57+
}
58+
$content | Add-Member -MemberType NoteProperty -Name "mounts" -Value @($mount)
59+
Write-Host "✅ Configured Azure CLI mounting for Windows" -ForegroundColor Green
60+
} "2" {
61+
Write-Host "✅ Configured for manual Azure CLI login with tenant/subscription specification" -ForegroundColor Green
62+
Write-Host " You'll need to run tenant-specific login after container startup:" -ForegroundColor Yellow
63+
Write-Host " az login --tenant <your-tenant-id-or-domain>" -ForegroundColor Yellow
64+
Write-Host " az account set --subscription <your-subscription-id-or-name>" -ForegroundColor Yellow
65+
Write-Host " az account show # Verify your context" -ForegroundColor Yellow
66+
}
67+
"3" {
68+
Write-Host "✅ No automatic configuration applied" -ForegroundColor Green
69+
Write-Host " You can manually edit .devcontainer/devcontainer.json later" -ForegroundColor Yellow
70+
}
71+
}
72+
73+
# Save the updated configuration
74+
$content | ConvertTo-Json -Depth 10 | Set-Content $devcontainerPath -Encoding UTF8
75+
Write-Host "✅ devcontainer.json updated successfully" -ForegroundColor Green
76+
77+
} catch {
78+
Write-Host "❌ Failed to update devcontainer.json: $_" -ForegroundColor Red
79+
exit 1
80+
}
81+
82+
Write-Host "`n🎉 Configuration completed successfully!" -ForegroundColor Green
83+
84+
switch ($choice) {
85+
"1" {
86+
Write-Host "`n📋 Next steps:" -ForegroundColor Cyan
87+
Write-Host "1. Rebuild your dev container" -ForegroundColor White
88+
Write-Host "2. Your local Azure CLI authentication will be available" -ForegroundColor White
89+
} "2" {
90+
Write-Host "`n📋 Next steps:" -ForegroundColor Cyan
91+
Write-Host "1. Start/rebuild your dev container" -ForegroundColor White
92+
Write-Host "2. Sign in with tenant-specific authentication:" -ForegroundColor White
93+
Write-Host " az login --tenant <your-tenant-id-or-domain>" -ForegroundColor White
94+
Write-Host " az account set --subscription <your-subscription-id-or-name>" -ForegroundColor White
95+
Write-Host " az account show # Verify your context" -ForegroundColor White
96+
}
97+
"3" {
98+
Write-Host "`n📋 Next steps:" -ForegroundColor Cyan
99+
Write-Host "1. Edit .devcontainer/devcontainer.json manually if needed" -ForegroundColor White
100+
Write-Host "2. See the commented examples in the file" -ForegroundColor White
101+
}
102+
}

0 commit comments

Comments
 (0)