Advanced usage patterns, worktree workflows, and multi-service configurations for Claude DevContainer.
- Worktree Workflows
- Multi-Service Development
- CLI Reference
- Advanced Configuration
- Custom Images
- Performance Optimization
Critical: Always use the
wtcommand to create worktrees. Manualgit worktree addwill NOT configure DevContainers properly.
# 1. Create main project with DevContainer
cd your-project
cdc init
# 2. Create feature worktrees for parallel development
cdc wt feature-auth # or: wt feature-auth
cdc wt bugfix-login # or: wt bugfix-login
# This creates:
# ../your-project-feature-auth/
# ../your-project-bugfix-login/# Create worktree from specific branch
wt feature-branch --from main
wt hotfix-urgent --from v1.2.3
# Each worktree gets its own DevContainer configuration
cd ../your-project-feature-branch
code . # Opens with proper DevContainer setupThe wt command automatically configures these environment variables:
WORKTREE_HOST_MAIN_REPO=/path/to/main/repo
WORKTREE_HOST_CURRENT_REPO=/path/to/current/worktree
WORKTREE_MAIN_REPO_NAME=your-project
WORKTREE_CURRENT_REPO_NAME=your-project-feature-nameUse these in your scripts and configurations for worktree-aware behavior.
Automatic Cleanup (Recommended)
# After merging feature back to main:
git merge feature-branch
# Post-merge hook automatically prompts for cleanupManual Cleanup
# List all worktrees and Docker artifacts
cdc cleanup --list
# Clean specific worktree
cdc cleanup feature-auth
# Clean all merged branches (safe)
cdc cleanup --merged
# Interactive cleanup with choices
cdc cleanup --interactive
# Preview cleanup without executing
cdc cleanup --dry-run --all- Use descriptive names:
wt feature-user-authinstead ofwt fix - Clean up regularly: Use
cdc cleanup --mergedafter releases - Isolate features: One feature per worktree for cleaner development
- Test before merge: Each worktree has isolated container environment
Choose multi-service when your project needs:
- Databases: PostgreSQL, MongoDB, or other data stores
- Caching: Redis or similar caching layers
- Background Jobs: Queues, workers, or scheduled tasks
- Full-Stack Apps: Complete web applications with multiple components
# List available templates
cdc services
# Web application with database
cdc compose web-db
# Creates: Next.js app + PostgreSQL + Redis
# Python ML with services
cdc compose python-ml-services
# Creates: Python ML + Vector DB + Redis Stack + Jupyter
# Complete full-stack application
cdc compose fullstack-nextjs
# Creates: Next.js + Worker + PostgreSQL + Redis + Mail + S3┌─────────────────┐ ┌──────────────────┐
│ VS Code │────│ Primary Service │ ← VS Code connects here
│ (Local) │ │ "app" │ (Claude Code + dev tools)
└─────────────────┘ │ - Claude Code │
│ - Source code │
│ - Build tools │
└─────────┬────────┘
│ Docker network
│
┌────────────┼────────────┐
│ │ │
┌─────▼────┐ ┌─────▼────┐ ┌────▼─────┐
│PostgreSQL│ │ Redis │ │Background│
│Service │ │Service │ │Worker │
└──────────┘ └──────────┘ └──────────┘
Connecting to Services
# Inside the primary "app" container:
# Connect to PostgreSQL
psql -h postgres -U myuser -d mydb
# Connect to Redis
redis-cli -h redis
# Check service status
docker-compose psService Configuration
Services are defined in docker-compose.yml and can be customized:
services:
app:
# Your primary development container
image: claude-nextjs:latest
# ...
postgres:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypass
redis:
image: redis:7-alpineMulti-service setups automatically configure connection variables:
DATABASE_URL=postgresql://user:pass@postgres:5432/mydb
REDIS_URL=redis://redis:6379
# ... other service URLs# Initialize new DevContainer
cdc init [--stack <stack>]
cdc init --stack python-ml
cdc init --stack nextjs
# Multi-service initialization
cdc compose <template>
cdc compose web-db
cdc compose python-ml-services
# Worktree operations (⚠️ Use ONLY these commands)
cdc wt <name> [--from <branch>]
wt <name> # Standalone command# Analyze existing DevContainer
cdc check
# Upgrade to latest features
cdc migrate [--dry-run]
# Detect project type
cdc detect
# List available stacks
cdc stacks
# List multi-service templates
cdc services# List worktrees and artifacts
cdc cleanup --list
# Clean specific worktree
cdc cleanup <worktree-name>
# Clean merged branches
cdc cleanup --merged [--verbose]
# Interactive cleanup
cdc cleanup --interactive
# Dry run (preview only)
cdc cleanup --dry-run [--all|--merged]# Global options
--help, -h Show help
--version, -v Show version
--verbose Verbose output
--dry-run Preview changes without executing
# Init options
--stack <name> Specify development stack
--force Overwrite existing configuration
# Cleanup options
--all Clean all worktrees (careful!)
--merged Clean only merged branches
--interactive Choose what to clean
--list List worktrees and artifacts
--verbose Show cleanup detailsDevContainer configurations support additional features:
{
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"./src/claude-mcp": {
"servers": "serena,context7,custom-server"
}
}
}{
"containerEnv": {
"NODE_ENV": "development",
"DEBUG": "*",
"CUSTOM_VAR": "value"
},
"remoteEnv": {
"PATH": "${containerEnv:PATH}:/custom/bin"
}
}{
"mounts": [
// Claude authentication (choose security level)
"source=${localEnv:HOME}/.claude,target=/home/claude-user/.claude,type=bind",
// Custom tool configurations
"source=${localEnv:HOME}/.gitconfig,target=/home/claude-user/.gitconfig,type=bind,readonly",
// Project-specific mounts
"source=${localWorkspaceFolder}/data,target=/workspace/data,type=bind"
]
}{
"hostRequirements": {
"cpus": 4,
"memory": "8gb",
"storage": "32gb"
}
}Extend existing Claude images for specialized environments:
# Dockerfile
FROM claude-python-ml:latest
USER root
# Install additional system packages
RUN apt-get update && apt-get install -y \
postgresql-client \
redis-tools \
&& rm -rf /var/lib/apt/lists/*
USER claude-user
# Install user-level tools
RUN pip install custom-package
RUN npm install -g custom-cli-tool
WORKDIR /workspace# Build your custom image
docker build -t my-custom-claude:latest .
# Use in DevContainer
cdc init --stack custom
# Edit .devcontainer/devcontainer.json:
# "image": "my-custom-claude:latest"- Always extend Claude base images for compatibility
- Follow USER pattern: root for system packages, claude-user for user tools
- Clean package caches to keep images lean
- Document customizations in your project README
- Version your custom images for reproducibility
For Development:
{
"hostRequirements": {
"cpus": 4,
"memory": "8gb"
}
}For ML/AI Workloads:
{
"hostRequirements": {
"cpus": 8,
"memory": "16gb",
"gpu": true
}
}Build only what you need:
# Instead of building all images (~4GB)
./build-all-images.sh --rebuild
# Build specific stack (~2GB)
./build-all-images.sh --images claude-base,python-mlUse parallel builds:
./build-all-images.sh --parallel --images claude-base,nextjsFaster startups:
- Use specific images instead of generic base
- Minimize mounted directories
- Cache dependencies in image layers
Resource efficiency:
- Set appropriate CPU/memory limits
- Use multi-stage builds for custom images
- Clean up unused Docker resources regularly
- Use worktrees for parallel feature development
- Clean up merged branches to prevent resource accumulation
- Use multi-service only when actually needed
- Monitor Docker resource usage with
docker system df
# .github/workflows/devcontainer-test.yml
name: Test DevContainer
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build images
run: ./build-all-images.sh --images claude-base,nextjs
- name: Test DevContainer
run: cdc init --stack nextjs# Shared team configuration
cdc init --stack nextjs
git add .devcontainer/
git commit -m "Add DevContainer configuration"
# Team members can then:
git pull
code . # "Dev Containers: Reopen in Container"Create reusable project templates with DevContainer configurations:
# In your template repository
cdc init --stack python-ml
# Customize configuration
# Commit and use as template- Security: Review SECURITY.md for production considerations
- Troubleshooting: See TROUBLESHOOTING.md for common issues
- Contributing: Check CONTRIBUTING.md to contribute improvements
- Setup: Return to SETUP.md for installation details