Skip to content

Commit bca6d47

Browse files
Add Prebuild Configuration information to the DevContainer Readme
1 parent 2d49942 commit bca6d47

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

.devcontainer/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This directory contains the optimized dev container configuration for the Azure
88
- [Files in this Directory](#files-in-this-directory)
99
- [Setup Stages](#setup-stages)
1010
- [Optimization Strategy](#optimization-strategy)
11+
- [Prebuild Configuration](#prebuild-configuration)
1112
- [Jupyter Kernel Configuration](#jupyter-kernel-configuration)
1213
- [Troubleshooting](#troubleshooting)
1314
- [Performance Notes](#performance-notes)
@@ -125,6 +126,127 @@ A few extensions like GitHub Copilot and Copilot Chat are still installed at con
125126

126127
This pre-installation happens in the Dockerfile and significantly reduces container startup time as VS Code doesn't need to download and install these extensions.
127128

129+
## 🏗️ Prebuild Configuration
130+
131+
### What is Devcontainer Prebuild?
132+
133+
**Devcontainer prebuild** is a GitHub Codespaces feature that pre-builds and caches container images with all dependencies and setup already completed. Instead of building the container from scratch every time someone opens a Codespace, GitHub builds and caches the container image ahead of time.
134+
135+
### How Prebuild Works
136+
137+
1. **Automatic Detection**: GitHub monitors changes to devcontainer configuration files (`.devcontainer/devcontainer.json`, `.devcontainer/Dockerfile`, etc.)
138+
2. **Triggered Builds**: When changes are detected, GitHub automatically starts a prebuild process
139+
3. **Full Setup Execution**: Runs the complete container build including:
140+
- Dockerfile instructions
141+
- `onCreateCommand` (virtual environment creation, package installation)
142+
- `updateContentCommand` (dependency updates, environment configuration)
143+
4. **Image Caching**: Stores the resulting container image in GitHub's registry
144+
5. **Fast Deployment**: When users open a Codespace, they get the pre-built image
145+
146+
### Prebuild Benefits
147+
148+
| Aspect | Without Prebuild | With Prebuild |
149+
|--------|------------------|---------------|
150+
| **Startup Time** | 5-10 minutes | 10-30 seconds |
151+
| **User Experience** | Watch installation progress | See verification only |
152+
| **Resource Usage** | Build every time | Build once, use many times |
153+
| **Consistency** | May vary due to network/timing | Identical pre-configured environment |
154+
| **Reliability** | Dependent on real-time installs | Pre-validated, cached environment |
155+
156+
### Prebuild Lifecycle Commands in This Project
157+
158+
Our devcontainer uses two key lifecycle commands optimized for prebuild:
159+
160+
#### `onCreateCommand` (Container Creation)
161+
```bash
162+
# Creates Python virtual environment and registers Jupyter kernel
163+
echo '🚀 Creating Python virtual environment in workspace...' &&
164+
/usr/local/bin/python3.12 -m venv /workspaces/Apim-Samples/.venv --copies &&
165+
source /workspaces/Apim-Samples/.venv/bin/activate &&
166+
pip install --upgrade pip setuptools wheel ipykernel &&
167+
python -m ipykernel install --user --name=apim-samples --display-name='APIM Samples Python 3.12'
168+
```
169+
170+
#### `updateContentCommand` (Content Updates)
171+
```bash
172+
# Installs Python packages and configures environment
173+
source /workspaces/Apim-Samples/.venv/bin/activate &&
174+
pip install -r requirements.txt &&
175+
pip install pytest pytest-cov coverage &&
176+
python setup/setup_python_path.py --generate-env &&
177+
az config set core.login_experience_v2=off &&
178+
az extension add --name containerapp --only-show-errors &&
179+
az extension add --name front-door --only-show-errors
180+
```
181+
182+
### When Prebuild is Triggered
183+
184+
Prebuild automatically occurs when you push changes to:
185+
- `.devcontainer/devcontainer.json`
186+
- `.devcontainer/Dockerfile`
187+
- `requirements.txt` (when referenced in `updateContentCommand`)
188+
- Any other files referenced in lifecycle commands
189+
190+
### Monitoring Prebuild Status
191+
192+
You can monitor prebuild status in several ways:
193+
194+
1. **GitHub Repository**:
195+
- Go to your repository on GitHub
196+
- Navigate to the "Code" tab
197+
- Look for the "Codespaces" section
198+
- Click on "View all" to see prebuild status
199+
200+
2. **Codespaces Settings**:
201+
- Visit [github.com/codespaces](https://github.com/codespaces)
202+
- Check the "Repository prebuilds" section
203+
- View build logs and status
204+
205+
3. **Build Indicators**:
206+
- ✅ Green checkmark: Prebuild successful
207+
- ❌ Red X: Prebuild failed
208+
- 🟡 Yellow circle: Prebuild in progress
209+
- ⚪ Gray circle: No recent prebuild
210+
211+
### Refreshing Prebuilt Containers
212+
213+
To refresh the prebuilt container (recommended periodically):
214+
215+
#### Method 1: Trigger via Configuration Change
216+
1. Make a small change to `.devcontainer/devcontainer.json` (e.g., add a comment)
217+
2. Commit and push the change
218+
3. GitHub will automatically trigger a new prebuild
219+
220+
#### Method 2: Manual Trigger (if available)
221+
1. Go to your repository's Codespaces settings
222+
2. Find the prebuild configuration
223+
3. Click "Trigger prebuild" if the option is available
224+
225+
#### Method 3: Update Dependencies
226+
1. Update `requirements.txt` with newer package versions
227+
2. Commit and push the changes
228+
3. Prebuild will automatically run with updated dependencies
229+
230+
### Best Practices for Prebuild
231+
232+
1. **Keep Commands Idempotent**: Ensure commands can run multiple times safely
233+
2. **Use Caching**: Leverage Docker layer caching and package managers' cache
234+
3. **Minimize Build Time**: Move heavy operations to prebuild, keep runtime light
235+
4. **Test Changes**: Verify prebuild success before merging configuration changes
236+
5. **Monitor Logs**: Check prebuild logs for warnings or potential optimizations
237+
6. **Regular Refresh**: Refresh prebuilds monthly or when dependencies change significantly
238+
239+
### Prebuild vs Runtime Separation
240+
241+
| Operation | Prebuild Stage | Runtime Stage |
242+
|-----------|----------------|---------------|
243+
| Python packages | ✅ Install all | ❌ Verify only |
244+
| Virtual environment | ✅ Create and configure | ❌ Activate only |
245+
| Azure CLI extensions | ✅ Install | ❌ Verify only |
246+
| Jupyter kernel | ✅ Register | ❌ Validate only |
247+
| Environment files | ✅ Generate | ❌ Check existence |
248+
| VS Code extensions | ✅ Install | ❌ Load only |
249+
128250
## 🔧 Jupyter Kernel Configuration
129251

130252
The dev container is configured with a custom Jupyter kernel for optimal Python development experience:
@@ -230,6 +352,25 @@ The post-start script provides real-time feedback:
230352
2. **Wait for Completion**: Let the verification finish before starting work
231353
3. **Check Status Messages**: Review any warnings or errors reported
232354
4. **Use Fallback Commands**: If something fails, the script provides guidance
355+
5. **Refresh Prebuilds Regularly**: Update prebuilt containers monthly or when major dependency changes occur
356+
357+
### Prebuild Refresh Recommendations
358+
359+
**When to refresh prebuilds**:
360+
- Monthly maintenance (keep dependencies current)
361+
- After major Python package updates
362+
- When Azure CLI or extensions have significant updates
363+
- If startup performance degrades over time
364+
- Before important development cycles or team onboarding
365+
366+
**Quick refresh method**:
367+
```bash
368+
# Add a comment to trigger prebuild
369+
# Edit .devcontainer/devcontainer.json and add/update a comment, then:
370+
git add .devcontainer/devcontainer.json
371+
git commit -m "Trigger prebuild refresh"
372+
git push
373+
```
233374

234375
---
235376

0 commit comments

Comments
 (0)