Mark Pitt
claude-skills
  1. claude-skills
  2. Developer environment
  3. devcontainers
  4. references/features-templates.md

A file in devcontainers

references/features-templates.md

Markdown 8.7 KB
features-templates.mdMarkdown, rendered

Dev Container Features and Templates

Reference for consuming Features, authoring custom Features, and distributing Templates.

---

Dev Container Features

Features are self-contained, versioned units of installation code that add tools and configuration to a dev container without modifying the Dockerfile. They are the recommended way to compose development environments.

Consuming Features

Add Features to the features object in devcontainer.json:

{
  "features": {
    "ghcr.io/devcontainers/features/node:1": { "version": "20" },
    "ghcr.io/devcontainers/features/python:1": { "version": "3.12" },
    "ghcr.io/devcontainers/features/git:1": {},
    "ghcr.io/devcontainers/features/azure-cli:1": { "version": "latest" },
    "ghcr.io/devcontainers/features/docker-in-docker:2": {
      "version": "latest",
      "dockerDashComposeVersion": "v2"
    }
  }
}

Always pin a major version (:1, :2) — never use :latest for production environments. The registry resolves the latest patch within a major version.

Popular Official Features

FeatureRegistry PathOptions
Node.jsghcr.io/devcontainers/features/node:1version
Pythonghcr.io/devcontainers/features/python:1version, installTools
Goghcr.io/devcontainers/features/go:1version
Rustghcr.io/devcontainers/features/rust:1version, profile
.NETghcr.io/devcontainers/features/dotnet:2version
Javaghcr.io/devcontainers/features/java:1version, jdkDistro
Gitghcr.io/devcontainers/features/git:1version, ppa
Docker-in-Dockerghcr.io/devcontainers/features/docker-in-docker:2version, moby
Docker-outside-Dockerghcr.io/devcontainers/features/docker-outside-of-docker:1
kubectl + Helmghcr.io/devcontainers/features/kubectl-helm-minikube:1version
Azure CLIghcr.io/devcontainers/features/azure-cli:1version
AWS CLIghcr.io/devcontainers/features/aws-cli:1version
GitHub CLIghcr.io/devcontainers/features/github-cli:1version

Browse the full registry: https://containers.dev/features

Controlling Install Order

Features install in an unspecified order by default. Use overrideFeatureInstallOrder when ordering matters:

{
  "features": {
    "ghcr.io/devcontainers/features/node:1": {},
    "ghcr.io/myorg/features/my-tool:1": {}
  },
  "overrideFeatureInstallOrder": [
    "ghcr.io/devcontainers/features/node:1",
    "ghcr.io/myorg/features/my-tool:1"
  ]
}

Alternatively, within your own Feature's devcontainer-feature.json, use installsAfter to declare soft dependencies.

---

Authoring Custom Features

Structure

my-feature/
├── devcontainer-feature.json   # Metadata and options
├── install.sh                  # Installation script (required)
└── README.md                   # Optional documentation

devcontainer-feature.json

{
  "id": "my-tool",
  "version": "1.0.0",
  "name": "My Tool",
  "description": "Installs My Tool for development",
  "documentationURL": "https://github.com/myorg/my-feature",
  "licenseURL": "https://github.com/myorg/my-feature/blob/main/LICENSE",
  "options": {
    "version": {
      "type": "string",
      "default": "latest",
      "description": "Version of My Tool to install",
      "proposals": ["latest", "1.0.0", "0.9.0"]
    },
    "installGlobalTools": {
      "type": "boolean",
      "default": true,
      "description": "Install global CLI tools alongside My Tool"
    }
  },
  "installsAfter": [
    "ghcr.io/devcontainers/features/common-utils"
  ],
  "containerEnv": {
    "MY_TOOL_HOME": "/usr/local/my-tool"
  }
}

install.sh Best Practices

#!/bin/bash
set -e

# Access options (injected as environment variables by CLI)
VERSION="${VERSION:-"latest"}"
INSTALL_GLOBAL_TOOLS="${INSTALLGLOBALTOOLS:-"true"}"

# Detect OS and architecture
. /etc/os-release
ARCH="$(uname -m)"
case "${ARCH}" in
  x86_64) ARCH="amd64" ;;
  aarch64 | arm64) ARCH="arm64" ;;
  *) echo "Unsupported architecture: ${ARCH}"; exit 1 ;;
esac

# Install dependencies
apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates

# Resolve 'latest' version
if [ "${VERSION}" = "latest" ]; then
  VERSION=$(curl -sf "https://api.github.com/repos/myorg/my-tool/releases/latest" \
    | grep '"tag_name"' | sed -E 's/"tag_name": "v?([^"]+)".*/\1/')
fi

# Download and install
curl -fsSL "https://example.com/releases/${VERSION}/my-tool-${ARCH}" \
  -o /usr/local/bin/my-tool
chmod +x /usr/local/bin/my-tool

# Respect remoteUser (CLI provides _REMOTE_USER variable)
if [ "${INSTALL_GLOBAL_TOOLS}" = "true" ]; then
  su "${_REMOTE_USER}" -c "my-tool install-globals"
fi

echo "Done! Installed my-tool ${VERSION}"

Key Authoring Rules

  1. Idempotency — scripts may run multiple times; check before acting
  2. OS/arch detection — handle amd64 and arm64 at minimum; detect distro via /etc/os-release
  3. Respect _REMOTE_USER — use the _REMOTE_USER variable (injected by CLI) when installing user-scoped tools
  4. Clean up after apt-get — add && rm -rf /var/lib/apt/lists/* to keep layers small
  5. Export env vars — use containerEnv in metadata rather than sourcing .bashrc (more reliable across shells)

Testing Features

Use the official test framework via the Dev Container CLI:

# Install CLI
npm install -g @devcontainers/cli

# Test against a specific base image
devcontainer features test \
  --features my-feature \
  --base-image mcr.microsoft.com/devcontainers/base:ubuntu \
  .

# Test against multiple images (define in devcontainer-feature.json's "scenarios")
devcontainer features test .

Publishing Features

# Build and push to GitHub Container Registry
devcontainer features publish \
  --registry ghcr.io \
  --namespace myorg \
  ./my-feature

The Feature becomes available at ghcr.io/myorg/features/my-feature:1.

---

Dev Container Templates

Templates are complete devcontainer.json configurations (plus optional Dockerfiles and Compose files) that define standardised starting points for new projects.

Using a Template

Via VS Code: Dev Containers: Add Dev Container Configuration Files → browse Templates

Via CLI:

devcontainer templates apply \
  --template-id ghcr.io/devcontainers/templates/python:1 \
  --workspace-folder .

Popular Official Templates

TemplateID
Python 3ghcr.io/devcontainers/templates/python:1
Node.jsghcr.io/devcontainers/templates/javascript-node:1
Goghcr.io/devcontainers/templates/go:1
Rustghcr.io/devcontainers/templates/rust:1
.NETghcr.io/devcontainers/templates/dotnet:1
Javaghcr.io/devcontainers/templates/java:1

Browse the full registry: https://containers.dev/templates

Authoring Custom Templates

my-template/
├── devcontainer-template.json   # Metadata
├── .devcontainer/
│   ├── devcontainer.json        # The template configuration
│   └── Dockerfile               # (optional)
└── README.md

devcontainer-template.json:

{
  "id": "my-stack",
  "version": "1.0.0",
  "name": "My Stack",
  "description": "Development environment for My Stack projects",
  "documentationURL": "https://github.com/myorg/my-template",
  "licenseURL": "https://github.com/myorg/my-template/blob/main/LICENSE",
  "options": {
    "nodeVersion": {
      "type": "string",
      "default": "20",
      "description": "Node.js version",
      "proposals": ["20", "18", "16"]
    }
  },
  "platforms": ["linux/amd64", "linux/arm64"],
  "publisher": "myorg",
  "keywords": ["node", "typescript", "my-stack"]
}

Template Distribution

  1. Publish to GitHub Container Registry as OCI artifacts (tarballs)
  2. Add devcontainer-collection.json to the repository root for discovery
  3. Use semantic versioning: 1.0.0, 1.1.0, etc.
devcontainer templates publish \
  --registry ghcr.io \
  --namespace myorg \
  ./my-template

Centralised Team Repository Pattern

Host a team-devcontainers repository with:

team-devcontainers/
├── devcontainer-collection.json   # Metadata index
├── templates/
│   ├── backend-service/           # Java Spring Boot template
│   ├── frontend-app/              # React/TypeScript template
│   └── data-science/              # Python + Jupyter template
└── features/
    ├── internal-cli/              # Company CLI tool
    └── vpn-config/                # Internal network setup

Share the collection URL with the team; point the VS Code Add Config dialog to it.