|
| 1 | +--- |
| 2 | +description: "Discussion of the devcontainers CLI project architecture, conventions, and development patterns" |
| 3 | +--- |
| 4 | + |
| 5 | +# DevContainers CLI Project Instructions |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The DevContainers CLI (`@devcontainers/cli`) is a TypeScript-based Node.js project that implements the [Development Containers specification](https://containers.dev). It provides tooling for building, running, and managing development containers across different container runtimes and orchestrators. |
| 10 | + |
| 11 | +## Architecture |
| 12 | + |
| 13 | +### Core Components |
| 14 | + |
| 15 | +- **`src/spec-configuration/`** - Configuration parsing and validation for devcontainer.json |
| 16 | +- **`src/spec-node/`** - Node.js-specific implementations of the specification |
| 17 | +- **`src/spec-utils/`** - Shared utilities for specification handling |
| 18 | +- **`src/test/`** - Comprehensive test suites including container tests |
| 19 | + |
| 20 | +### Key Design Principles |
| 21 | + |
| 22 | +1. **Specification Compliance**: All features must align with the official devcontainer specification |
| 23 | +2. **Multi-Runtime Support**: Support for Docker, Podman, and other container runtimes |
| 24 | +3. **Cross-Platform**: Works on Windows, macOS, and Linux |
| 25 | +4. **Extensibility**: Plugin architecture for features and lifecycle hooks |
| 26 | + |
| 27 | +## Development Conventions |
| 28 | + |
| 29 | +### TypeScript Patterns |
| 30 | + |
| 31 | +- Use strict TypeScript configuration with `noImplicitAny` and `strictNullChecks` |
| 32 | +- Prefer interfaces over type aliases for object shapes |
| 33 | +- Use proper async/await patterns, avoid callback-style APIs |
| 34 | +- Export types and interfaces from dedicated `types.ts` files |
| 35 | + |
| 36 | +### Error Handling |
| 37 | + |
| 38 | +- Use custom error classes that extend base `Error` |
| 39 | +- Provide meaningful error messages with context |
| 40 | +- Include error codes for programmatic handling |
| 41 | +- Log errors at appropriate levels using the project's logging system |
| 42 | + |
| 43 | +### Testing Strategy |
| 44 | + |
| 45 | +- Unit tests in `src/test/` with `.test.ts` suffix |
| 46 | +- Container integration tests that actually build and run containers |
| 47 | +- Mock external dependencies (Docker API, file system operations) |
| 48 | +- Use descriptive test names that explain the scenario being tested |
| 49 | + |
| 50 | +## Key Integration Points |
| 51 | + |
| 52 | +### Container Runtime Integration |
| 53 | + |
| 54 | +- **Docker**: Primary runtime support via Docker API |
| 55 | +- **Podman**: Alternative runtime with compatibility layer |
| 56 | +- **BuildKit**: For advanced build features and caching |
| 57 | + |
| 58 | +### File System Operations |
| 59 | + |
| 60 | +- Configuration discovery and parsing from workspace roots |
| 61 | +- Template and feature resolution from local and remote sources |
| 62 | +- Volume mounting and bind mount handling across platforms |
| 63 | + |
| 64 | +### External Dependencies |
| 65 | + |
| 66 | +- **Container registries**: For pulling base images and publishing |
| 67 | +- **Git repositories**: For fetching features and templates |
| 68 | +- **Package managers**: npm, pip, apt for installing tools in containers |
| 69 | + |
| 70 | +## Common Development Patterns |
| 71 | + |
| 72 | +### Adding New CLI Commands |
| 73 | + |
| 74 | +1. Define command in `src/spec-node/devContainersSpecCLI.ts` |
| 75 | +2. Implement handler function with proper argument parsing |
| 76 | +3. Add comprehensive error handling and logging |
| 77 | +4. Include unit and integration tests |
| 78 | +5. Update CLI help text and documentation |
| 79 | + |
| 80 | +### Configuration Processing |
| 81 | + |
| 82 | +- Use `src/spec-configuration/` utilities for parsing devcontainer.json |
| 83 | +- Validate configuration against JSON schema |
| 84 | +- Handle inheritance and composition (extends, merging) |
| 85 | +- Support both local and remote configuration sources |
| 86 | + |
| 87 | +### Feature Implementation |
| 88 | + |
| 89 | +- Follow the specification's feature model |
| 90 | +- Support installation scripts and lifecycle hooks |
| 91 | +- Handle dependency resolution and ordering |
| 92 | +- Provide proper cleanup and error recovery |
| 93 | + |
| 94 | +## Common Pitfalls |
| 95 | + |
| 96 | +### Platform-Specific Issues |
| 97 | + |
| 98 | +- **Path handling**: Use `path.posix` for container paths, `path` for host paths |
| 99 | +- **Line endings**: Handle CRLF/LF differences in scripts and configs |
| 100 | +- **File permissions**: Different behavior on Windows vs Unix systems |
| 101 | +- **Container mounting**: Volume vs bind mount differences across platforms |
| 102 | + |
| 103 | +### Container Runtime Differences |
| 104 | + |
| 105 | +- Docker Desktop vs Docker Engine behavior variations |
| 106 | +- Podman compatibility quirks (networking, volumes, security contexts) |
| 107 | +- Image building differences between runtimes |
| 108 | +- Registry authentication handling |
| 109 | + |
| 110 | +### Performance Considerations |
| 111 | + |
| 112 | +- **Image caching**: Leverage BuildKit and registry caching |
| 113 | +- **Parallel operations**: Use proper concurrency for multi-container scenarios |
| 114 | +- **File watching**: Efficient change detection for rebuild scenarios |
| 115 | +- **Network optimization**: Minimize registry pulls and DNS lookups |
| 116 | + |
| 117 | +## Development Workflow |
| 118 | + |
| 119 | +### Setup and Building |
| 120 | + |
| 121 | +```bash |
| 122 | +# Install dependencies |
| 123 | +npm install |
| 124 | + |
| 125 | +# Build TypeScript |
| 126 | +npm run build |
| 127 | + |
| 128 | +# Run tests |
| 129 | +npm test |
| 130 | + |
| 131 | +# Run container tests (requires Docker) |
| 132 | +npm run test:container |
| 133 | +``` |
| 134 | + |
| 135 | +### Testing Guidelines |
| 136 | + |
| 137 | +- Always test with actual containers, not just mocks |
| 138 | +- Test cross-platform scenarios when possible |
| 139 | +- Include negative test cases for error conditions |
| 140 | +- Verify cleanup behavior (containers, volumes, networks) |
| 141 | + |
| 142 | +### Debugging |
| 143 | + |
| 144 | +- Use `--log-level trace` for detailed operation logging |
| 145 | +- Container logs are available via runtime APIs |
| 146 | +- File system operations are logged at debug level |
| 147 | +- Network issues often manifest as timeout errors |
| 148 | + |
| 149 | +## Extension Points |
| 150 | + |
| 151 | +### Custom Features |
| 152 | + |
| 153 | +- Implement in separate npm packages |
| 154 | +- Follow feature specification format |
| 155 | +- Provide proper metadata and documentation |
| 156 | +- Test with multiple base images and scenarios |
| 157 | + |
| 158 | +### Lifecycle Hooks |
| 159 | + |
| 160 | +- `onCreateCommand`, `postCreateCommand`, `postStartCommand` |
| 161 | +- Handle both synchronous and asynchronous operations |
| 162 | +- Provide proper error propagation |
| 163 | +- Support both shell commands and executable scripts |
| 164 | + |
| 165 | +## Related Documentation |
| 166 | + |
| 167 | +- [Contributing Guidelines](../../CONTRIBUTING.md) |
| 168 | +- [Development Container Specification](https://containers.dev) |
| 169 | +- [Feature and Template specifications](https://containers.dev/implementors/features/) |
| 170 | +- [JSON Schema definitions](src/spec-configuration/schemas/) |
| 171 | + |
| 172 | +## Key Files and Directories |
| 173 | + |
| 174 | +- `src/spec-node/devContainersSpecCLI.ts` - Main CLI entry point |
| 175 | +- `src/spec-configuration/configuration.ts` - Configuration parsing logic |
| 176 | +- `src/spec-utils/` - Shared utilities and helpers |
| 177 | +- `src/test/container-features/` - Feature integration tests |
| 178 | +- `.devcontainer/` - Project's own development container configuration |
0 commit comments