Containers and icons (D2)

Beyond nesting, D2 has a small styling vocabulary that is enough to make a diagram look deliberate without a stylesheet.


#What this example shows

  • Named containers with display labels.

  • Shape keywords, which change how a node is drawn.

  • Per-node style blocks for fill and stroke.


#A worked example

Open it, edit it, or copy the source below.

Containers and icons (D2)

The source, which you can paste into a new diagram and edit:

direction: down

team: Platform team {
  style: {
    fill: "#f8fafc"
    stroke: "#94a3b8"
  }

  build: Build pipeline {
    shape: hexagon
  }
  registry: Image registry {
    shape: cylinder
  }
  deploy: Deployer {
    shape: hexagon
  }
}

clusters: Environments {
  staging: Staging {
    shape: package
  }
  prod: Production {
    shape: package
    style: {
      stroke: "#b91c1c"
      stroke-width: 2
    }
  }
}

team.build -> team.registry: push image
team.registry -> team.deploy: pull
team.deploy -> clusters.staging: deploy
clusters.staging -> clusters.prod: promote {
  style.stroke-dash: 4
}

#Making it your own

  • Shapes worth knowing: cylinder, hexagon, package, queue, document, person.

  • Style blocks nest, so a container style applies as a background to its children.

  • stroke-dash on a connection is the tidy way to mark something conditional.



Nesting is the whole idea.