Dev container

From j4flmao/agent-skills · View interactive version

Dev container is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about devcontainer.json, Dev Containers in VS Code, GitHub Codespaces, a reproducible Docker-based development environment or dev container features. Produces devcontainer config, Dockerfile and compose setup for local development. Not for production images (use docker-pattern

How to use this skill

Skill file

Dev Container

Purpose

Create and configure development containers — Docker-based, consistent development environments defined by a devcontainer.json and Dockerfile. Dev containers ensure all team members (and CI) use identical tools, runtimes, and configurations, eliminating "works on my machine" issues.

Agent Protocol

Trigger

Exact user phrases: "dev container", "devcontainer.json", "development container", "VS Code Dev Container", "GitHub Codespaces", "dev environment setup", "reproducible dev environment", "containerized dev", "Docker dev environment", "remote container".

Input Context

Output Artifact

Complete dev container configuration with Dockerfile, devcontainer.json, and Docker Compose (if multi-service).

Response Format

Base: {image or Dockerfile}
Features: {ghcr.io/devcontainers/features/... list}
Services: {compose services | none}
Lifecycle: postCreateCommand {cmd}
Ports: {forwardPorts}
Files: .devcontainer/devcontainer.json {+ Dockerfile, compose.yaml}

Completion Criteria

Max Response Length

200 lines.

Framework/Methodology

Dev Container Decision Tree

What is the project type?
├── Single runtime, no services → Dockerfile-based
│   base: mcr.microsoft.com/devcontainers/typescript-node:24
│   → apt install tools → npm install → done
├── Single runtime + databases → Docker Compose
│   app container + postgres + redis containers
│   → depends_on, network, healthcheck
├── Polyglot / multiple runtimes → Feature-based
│   base: debian + features (node, python, go)
│   → ghcr.io/devcontainers/features/*
├── GPU / CUDA development → GPU-enabled base
│   base: mcr.microsoft.com/devcontainers/cuda
│   → nvidia-docker runtime
└── Embedded / IoT (Rust, C++) → Cross-compilation tools
    → arm-none-eabi-gcc, QEMU, platformio feature

Dev Container Architecture

Host Machine (VS Code, Codespaces, JetBrains)
    ↕ Dev Containers extension / Codespaces / devcontainer CLI
Container (Docker)
├── Application code (mounted volume)
├── Language runtime (Node, Python, Go, etc.)
├── Build tools (npm, cargo, make, etc.)
├── VS Code extensions (inside container)
├── Git credentials forwarded
├── SSH agent forwarded
├── Ports forwarded (app, debugger, services)
└── Docker-outside-Docker (optional)
    ↓
Docker Compose (optional multi-service)
├── App container
├── PostgreSQL / MySQL / MongoDB
├── Redis / Memcached
└── Other services

Workflow

Step 1: Create devcontainer.json

// .devcontainer/devcontainer.json
{
  "name": "My App Dev Container",

  // Option A: Use a pre-built dev container image
  "image": "mcr.microsoft.com/devcontainers/typescript-node:24",

  // Option B: Build from Dockerfile
  // "build": {
  //   "dockerfile": "Dockerfile",
  //   "args": { "VARIANT": "20" }
  // },

  // Option C: Use Docker Compose
  // "dockerComposeFile": "docker-compose.yml",
  // "service": "app",
  // "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

  // Features (additional tools from registry)
  // (the typescript-node image already ships Node; add the node feature only on generic bases)
  "features": {
    "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
    "ghcr.io/devcontainers/features/git:1": {},
    "ghcr.io/devcontainers/features/github-cli:1": {}
  },

  // Tool-specific settings live under "customizations" (top-level "extensions"/"settings" are deprecated)
  "customizations": {
    "vscode": {
      "extensions": [
        "dbaeumer.vscode-eslint",
        "esbenp.prettier-vscode",
        "bradlc.vscode-tailwindcss",
        "github.vscode-github-actions",
        "ms-azuretools.vscode-docker"
      ],
      "settings": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "esbenp.prettier-vscode",
        "typescript.updateImportsOnFileMove.enabled": "always"
      }
    }
  },

  // Ports to forward
  "forwardPorts": [3000, 5432, 6379],

  // Environment variables
  "remoteEnv": {
    "DATABASE_URL": "postgresql://user:pass@localhost:5432/myapp",
    "REDIS_URL": "redis://localhost:6379",
    "NODE_ENV": "development"
  },

  // Git credentials, ~/.gitconfig and the SSH agent are forwarded automatically by
  // VS Code Dev Containers / Codespaces — avoid bind-mounting ~/.ssh into the container.
  "mounts": [
    "source=${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume"
  ],

  // Commands to run after the container is created
  "postCreateCommand": "npm install && npm run build",
  "postStartCommand": "git config --global --add safe.directory ${containerWorkspaceFolder}",
  "postAttachCommand": "npm run dev"
}

Step 2: Custom Dockerfile

# .devcontainer/Dockerfile
ARG VARIANT="20"
FROM mcr.microsoft.com/devcontainers/typescript-node:${VARIANT}

# Install additional system packages
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
    && apt-get install -y --no-install-recommends \
        postgresql-client \
        redis-tools \
        ripgrep \
        fd-find \
        bat \
        httpie \
    && apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Install global npm tools
RUN su node -c "npm install -g pnpm tsx eslint prettier"

# Set up shell prompt customization
COPY --chown=node:node .zshrc /home/node/.zshrc

Step 3: Docker Compose for Multi-Service

# .devcontainer/docker-compose.yml  (Compose Specification: top-level `version` is obsolete)
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        VARIANT: "20"
    volumes:
      - ..:/workspaces:cached
    environment:
      DATABASE_URL: postgresql://user:pass@postgres:5432/myapp
      REDIS_URL: redis://redis:6379
    # Overrides default command so things don't shut down
    command: sleep infinity
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    user: node
    networks:
      - dev

  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: myapp
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user -d myapp"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - dev

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    networks:
      - dev

volumes:
  postgres-data:
  redis-data:

networks:
  dev:

Step 4: Post-Create Script

#!/bin/bash
# .devcontainer/post-create.sh
set -e

echo "🚀 Setting up development environment..."

# Install dependencies
echo "📦 Installing npm dependencies..."
npm install

# Copy environment file if not exists
if [ ! -f .env ]; then
    echo "🔧 Creating .env file from template..."
    cp .env.example .env
fi

# Run database migrations
echo "🗄️ Running database migrations..."
npm run db:migrate

# Seed development data
echo "🌱 Seeding database..."
npm run db:seed

# Install git hooks (husky v9: `husky` replaces the deprecated `husky install`)
echo "🔗 Installing git hooks..."
npx husky

echo "✅ Development environment ready!"

Step 5: GitHub Codespaces Config

// .devcontainer/devcontainer.json (Codespaces-optimized)
{
  "name": "My App",
  "image": "mcr.microsoft.com/devcontainers/typescript-node:24",

  // Codespaces machine type recommendation
  "hostRequirements": {
    "cpus": 4,
    "memory": "8gb",
    "storage": "32gb"
  },

  "features": {
    "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
  },

  "forwardPorts": [3000],
  "portsAttributes": {
    "3000": {
      "label": "Dev Server",
      "onAutoForward": "notify"
    }
  },

  // Codespaces secrets are injected as environment variables automatically;
  // "secrets" only prompts users to provide recommended ones when creating a codespace.
  "secrets": {
    "API_KEY": { "description": "API key for the staging backend" }
  },

  "onCreateCommand": ".devcontainer/post-create.sh",
  "waitFor": "onCreateCommand",

  "customizations": {
    "codespaces": {
      "openFiles": ["README.md", "src/index.ts"]
    }
  }
}

Step 6: JetBrains IDEs

JetBrains IDEs (via Gateway or the IDE's Dev Containers support) read the same devcontainer.json; VS Code extensions are ignored there, so install CLI tools in the Dockerfile or via features. IDE-specific options go under customizations.jetbrains (check the JetBrains Dev Containers docs for the supported keys in your IDE version).

{
  "name": "My App",
  "image": "mcr.microsoft.com/devcontainers/typescript-node:24",
  "forwardPorts": [3000, 5432],
  "customizations": {
    "jetbrains": { "backend": "WebStorm" }
  }
}

Step 7: CI Validation of Dev Container

# .github/workflows/devcontainer-ci.yml
name: Dev Container Validation
on:
  pull_request:
    paths:
      - '.devcontainer/**'

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build dev container
        uses: devcontainers/ci@v0.3
        with:
          imageName: ghcr.io/myorg/devcontainer
          cacheFrom: ghcr.io/myorg/devcontainer
          push: never
          runCmd: npm test   # runs inside the built container with the repo mounted

Common Pitfalls

| Pitfall | Description | Prevention |

|---------|-------------|------------|

| Running as root | Container user has file permission issues | Use remoteUser in devcontainer.json, match host UID |

| No git credential forwarding | Can't push to remote from container | Configure Git credential helper, SSH agent |

| State loss on rebuild | Database data lost when container rebuilds | Use Docker volumes for persistent data |

| Slow rebuilds | Installing everything from scratch | Use Docker layer caching, feature cache |

| Port conflicts | Multiple projects on same ports | Use different ports per project |

| Missing features | Common tools not installed | Use devcontainer-features registry |

| Large image size | GB-sized images slow to pull | Use slim base images, multi-stage builds |

| No healthcheck for services | App starts before DB is ready | Healthcheck + depends_on condition |

| Hardcoded UID/GID | Doesn't work across teams | Dynamic UID resolution in entrypoint |

| No .env file | Missing environment variables | Copy from .env.example automatically |

Best Practices

| Practice | Rationale |

|----------|-----------|

| Use Docker Compose for multi-service | DB, cache, and other services in isolated containers |

| Use devcontainer-features | Community-maintained, versioned tool installations |

| Pin base image tags | Avoid unexpected breaking changes from :latest |

| Set remoteUser | Match host file permissions |

| Configure git credentials | Without this, git operations fail inside container |

| Use postCreateCommand for setup | Automatically set up after first build |

| Healthcheck for DB services | App container waits for ready database |

| Rely on built-in credential/SSH-agent forwarding | Host git config and keys stay on the host |

| Keep .devcontainer in version control | All team members use the same setup |

| Test dev container in CI | Ensure it builds and works on every PR |

| Document .env variables | Team knows what needs configuring |

| Use .dockerignore | Exclude node_modules, .git from build context |

Architecture Patterns

Minikube/K3s Dev Container

{
  "features": {
    "ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {},
    "ghcr.io/devcontainers/features/docker-in-docker:2": {}
  },
  "postStartCommand": "minikube start --driver=docker"
}

Multi-root Workspace (Monorepo)

{
  "name": "My Monorepo",
  "image": "mcr.microsoft.com/devcontainers/javascript-node:24",
  "workspaceFolder": "/workspaces/myapp",
  "customizations": { "vscode": { "extensions": ["dbaeumer.vscode-eslint"] } },
  "postCreateCommand": "npm install --workspaces"
}

References

Handoff

Hand off to dev-loop-git-workflow for Git credential configuration. Hand off to dev-loop-security-auditor for container security.

Architecture Decision Trees

Development Environment Strategy

| Decision Point | Option A | Option B | Decision Criteria |

|---|---|---|---|

| Container base image | Slim distro (smaller pulls) | Full devcontainers image (tooling-rich) | Pull time vs out-of-box tooling |

| Package manager | apt + pip | nix/home-manager | Reproducibility vs learning curve |

| Shell | bash | zsh + oh-my-zsh | Portability vs productivity |

| Dotfiles management | Manual COPY in Dockerfile | chezmoi/ dotfiles repo | Simplicity vs flexibility |

Multi-Service Topology

Implementation Patterns

Dockerfile Optimization

`dockerfile

Multi-stage build for smaller final image

FROM node:24-alpine AS builder

WORKDIR /app

COPY package*.json ./

RUN npm ci --only=production

COPY . .

RUN npm run build

FROM node:24-alpine

WORKDIR /app

COPY --from=builder /app/dist ./dist

COPY --from=builder /app/node_modules ./node_modules

EXPOSE 3000

CMD ["node", "dist/server.js"]

`

DevContainer Features Configuration

`json

{

"name": "Full Stack Dev Container",

"image": "mcr.microsoft.com/devcontainers/typescript-node:24",

"features": {

"ghcr.io/devcontainers/features/docker-in-docker:2": {},

"ghcr.io/devcontainers/features/git-lfs:1": {},

"ghcr.io/devcontainers/features/sshd:1": {}

},

"postCreateCommand": "npm install && npm run build",

"remoteUser": "node",

"mounts": [

"source=C:\Users\Hi/.ssh,target=/home/node/.ssh,type=bind"

]

}

`

Production Considerations

Performance

Reliability

Anti-Patterns

| Anti-Pattern | Symptom | Solution |

|---|---|---|

| Dev container as production image | Bloated prod images | Use multi-stage builds, separate dev vs prod |

| Installing everything in one layer | Poor caching, slow rebuilds | Layer strategically by change frequency |

| Ignoring .dockerignore | Large build context, slow sends | Always include .dockerignore |

| Root user in container | Security risk, permission issues | Use dedicated non-root user |

| Hardcoded environment variables | Configuration drift between devs | Use .env file with .env.example template |

Performance Optimization

Build Speed

Runtime Speed

Security Considerations

Container Hardening

Supply Chain

Secrets Management

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.