Dev Container with Claude Code
Learn about Dev Container using Claude Code. Practical tips and code examples included.
Unified Development Environments With Dev Containers
Dev Containers are container-based development environments you can use with VS Code and GitHub Codespaces. By simply defining a .devcontainer directory, every member of your team can stand up the same dev environment instantly. Claude Code can help generate and optimize these configuration files accurately.
Basic Dev Container Configuration
Ask Claude Code to generate the configuration file.
> Create a Dev Container configuration for a TypeScript + Next.js project.
> Include PostgreSQL, Redis, recommended extensions, and Git settings.
// .devcontainer/devcontainer.json
{
"name": "Next.js Development",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "20"
},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"bradlc.vscode-tailwindcss",
"prisma.prisma",
"ms-azuretools.vscode-docker",
"eamodio.gitlens",
"usernamehw.errorlens",
"formulahendry.auto-rename-tag"
],
"settings": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"typescript.preferences.importModuleSpecifier": "non-relative",
"typescript.updateImportsOnFileMove.enabled": "always"
}
}
},
"forwardPorts": [3000, 5432, 6379],
"portsAttributes": {
"3000": { "label": "Next.js App", "onAutoForward": "openBrowser" },
"5432": { "label": "PostgreSQL" },
"6379": { "label": "Redis" }
},
"postCreateCommand": "npm install && npx prisma generate",
"postStartCommand": "npx prisma migrate dev --skip-seed",
"remoteUser": "node"
}
Docker Compose Integration
Using Docker Compose with a Dev Container.
# .devcontainer/docker-compose.yml
version: "3.9"
services:
app:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
volumes:
- ..:/workspace:cached
- node_modules:/workspace/node_modules
command: sleep infinity
environment:
- DATABASE_URL=postgresql://postgres:password@db:5432/myapp
- REDIS_URL=redis://redis:6379
- NODE_ENV=development
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
restart: unless-stopped
volumes:
- redis_data:/data
volumes:
node_modules:
postgres_data:
redis_data:
# .devcontainer/Dockerfile
FROM mcr.microsoft.com/devcontainers/typescript-node:20
# Install additional tools
RUN apt-get update && apt-get install -y \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
# Global packages
RUN npm install -g prisma @anthropic-ai/claude-code
Lifecycle Scripts
You can wire scripts into the Dev Container lifecycle.
{
"initializeCommand": "echo 'Initializing Dev Container...'",
"onCreateCommand": {
"install": "npm install",
"prisma": "npx prisma generate",
"husky": "npx husky install"
},
"updateContentCommand": "npm install",
"postCreateCommand": {
"migrate": "npx prisma migrate dev",
"seed": "npx prisma db seed"
},
"postStartCommand": "npm run dev",
"postAttachCommand": "echo 'Connected to Dev Container'"
}
Performance Optimization
Settings that improve Dev Container performance.
{
"mounts": [
{
"source": "devcontainer-node-modules-${devcontainerId}",
"target": "/workspace/node_modules",
"type": "volume"
},
{
"source": "devcontainer-next-cache-${devcontainerId}",
"target": "/workspace/.next",
"type": "volume"
}
],
"hostRequirements": {
"cpus": 4,
"memory": "8gb",
"storage": "32gb"
}
}
GitHub Codespaces Support
Extend the config so it also works on GitHub Codespaces.
{
"hostRequirements": {
"cpus": 4,
"memory": "8gb"
},
"secrets": {
"GITHUB_TOKEN": {
"description": "GitHub API token for authentication"
},
"DATABASE_URL": {
"description": "PostgreSQL connection string"
}
},
"customizations": {
"codespaces": {
"openFiles": ["README.md", "src/app/page.tsx"]
}
}
}
Team Workflows
Best practices when sharing Dev Container configuration across a team.
// Add to .devcontainer/devcontainer.json
{
"customizations": {
"vscode": {
"settings": {
// Unified team settings
"files.eol": "\n",
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.tabSize": 2,
"editor.insertSpaces": true,
// TypeScript settings
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
}
}
}
Summary
Dev Containers eliminate the “but it works on my machine” problem at its root. With Claude Code, you can efficiently build everything from Dockerfile to Docker Compose integration, VS Code settings, and performance optimization, all in one coherent effort.
For more on Docker Compose, see building a Docker Compose dev environment. For CI/CD integration, see the CI/CD setup guide. You should also check the official Dev Container specification.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Key commands, shortcuts, and prompt examples on a single printable page.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
Creating Custom Slash Commands in Claude Code — Tailor Your Workflow
Learn how to create custom slash commands in Claude Code. Covers file placement, arguments, and automating frequent tasks with practical code examples.
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Canvas/WebGL Optimization with Claude Code
Learn about Canvas/WebGL optimization using Claude Code. Practical tips and code examples included.