Part 3: Writing a topology file

containerlab
workshop
Describe a lab in one YAML file: nodes, kinds, links, management addresses and startup configurations. Build a routed four-node lab and verify it.
Author

Abid Ghufran

Published

November 7, 2026

A Containerlab lab is one YAML file, called the topology file. It lists the nodes, says what each one runs, and describes the cables between them. Containerlab reads it and builds the whole lab with one command.

In this part you build a small routed network: two Cisco XRd routers with a Linux host on each side. Then you verify that the hosts can reach each other through both routers. It assumes you finished Part 1 and Part 2, and have the XRd and Alpine images.

The lab you will build

h1 h2 xrd1 xrd2 Alpine host Alpine host Cisco XRd router Cisco XRd router 192.168.1.0/24 10.0.12.0/30 192.168.2.0/24 .10 — .1 .1 — .2 .1 — .10
Two routers with a host on each side. The labels on the links are the subnets and the last number of each end's address.

To see the lab drawn by the same tool you will use for your own files, open it in the Topology Viewer. Hover over a node for its kind, image and management address.

Node Kind Management address Data addresses
xrd1 Cisco XRd router 172.20.20.11 192.168.1.1 (to h1), 10.0.12.1 (to xrd2)
xrd2 Cisco XRd router 172.20.20.12 10.0.12.2 (to xrd1), 192.168.2.1 (to h2)
h1 Alpine Linux host 172.20.20.21 192.168.1.10, gateway 192.168.1.1
h2 Alpine Linux host 172.20.20.22 192.168.2.10, gateway 192.168.2.1

Each router has a static route to the far host network, so traffic from h1 to h2 crosses xrd1 and xrd2.

The parts of a topology file

name: routed            # lab name: used in container names and the lab folder

mgmt:                   # optional: the management network (default shown below)
  network: clab
  ipv4-subnet: 172.20.20.0/24

topology:
  defaults: {}          # optional: settings for every node
  kinds: {}             # optional: settings for every node of a kind
  groups: {}            # optional: named sets of settings that nodes can use
  nodes: {}             # the nodes
  links: []             # the cables between them
Key Required What it does
name Yes Names the lab. Containers are called clab-<name>-<node>, and generated files go in a folder clab-<name>. Keep it short: it is part of every container name
prefix No Changes the clab part of container names; "" uses bare node names
mgmt No Sets the management network. Leave it out to use the default clab network, 172.20.20.0/24
topology.nodes Yes One entry per node
topology.links No One entry per cable
topology.kinds, defaults, groups No Shared settings, so you do not repeat yourself

The lab’s topology file

This is routed.clab.yml from the workshop repository:

name: routed

topology:
  # Settings shared by every node of a kind.
  kinds:
    cisco_xrd:
      image: ios-xr/xrd-control-plane:7.11.2
    linux:
      image: alpine:3

  nodes:
    xrd1:
      kind: cisco_xrd
      mgmt-ipv4: 172.20.20.11
      startup-config: configs/xrd1.cfg
    xrd2:
      kind: cisco_xrd
      mgmt-ipv4: 172.20.20.12
      startup-config: configs/xrd2.cfg

    h1:
      kind: linux
      mgmt-ipv4: 172.20.20.21
      exec:
        - ip addr add 192.168.1.10/24 dev eth1
        - ip route add 192.168.2.0/24 via 192.168.1.1
    h2:
      kind: linux
      mgmt-ipv4: 172.20.20.22
      exec:
        - ip addr add 192.168.2.10/24 dev eth1
        - ip route add 192.168.1.0/24 via 192.168.2.1

  links:
    # Gi0-0-0-0 here is GigabitEthernet0/0/0/0 inside the router.
    - endpoints: ["h1:eth1", "xrd1:Gi0-0-0-0"]
    - endpoints: ["xrd1:Gi0-0-0-1", "xrd2:Gi0-0-0-1"]
    - endpoints: ["xrd2:Gi0-0-0-0", "h2:eth1"]

Open it in the topology viewer to see the lab drawn, with each node’s kind, image and management address when you hover over it. Paste your own topology files there as you write them: it applies kinds, defaults and groups, and flags links to nodes you have not defined.

You can also work the other way round: in the topology designer, draw the nodes and links, and it writes the topology file for you, with interface names filled in for each kind. Choose Start from… → Routed lab (Part 3) to open this lab there, or press Edit in designer in the viewer. It is a quick way to sketch a new lab; this part explains what each line of the file it produces means.

Kinds

kinds sets the image once for each kind of node, so the nodes only say which kind they are. To move every router to a new IOS XR release, you change one line.

Nodes

Each node needs a kind. The other settings used here:

Setting Used for
kind What the node is: cisco_xrd, arista_ceos, linux and so on. It decides interface names, login and how configuration is applied
image The Docker image (here inherited from kinds)
mgmt-ipv4 A fixed management address, so it is the same every time you deploy
startup-config A configuration file the node loads at boot. The path is relative to the topology file
exec Commands run inside the container once it has started; handy for Linux hosts

Other settings you will meet:

Setting Used for
type A model within a kind, for kinds that offer several, such as a chassis type
binds Mount files or folders from the host into the node, as host-path:container-path
ports Publish a node’s port on the host, as in docker run -p
env Environment variables for the node
startup-delay Wait this many seconds before starting the node; useful for big labs of heavy nodes
group Take settings from a named entry in topology.groups

When a setting appears at several levels, the most specific wins: node, then group, then kind, then defaults.

The startup configurations

configs/xrd1.cfg is the IOS XR configuration that xrd1 loads when it starts:

hostname xrd1
username clab
 group root-lr
 group cisco-support
 secret clab@123
!
line default
 transport input ssh
!
interface GigabitEthernet0/0/0/0
 description to h1
 ipv4 address 192.168.1.1 255.255.255.0
!
interface GigabitEthernet0/0/0/1
 description to xrd2
 ipv4 address 10.0.12.1 255.255.255.252
!
router static
 address-family ipv4 unicast
  192.168.2.0/24 10.0.12.2
  0.0.0.0/0 MgmtEth0/RP0/CPU0/0 {{ .MgmtIPv4Gateway }}
 !
!
ssh server v2
end
Lines What they do
username, line default and ssh server v2 The login and the SSH server, as in Part 2
The two interface blocks The addresses towards h1 and towards xrd2
192.168.2.0/24 10.0.12.2 The static route to h2’s network, through xrd2
0.0.0.0/0 MgmtEth0/RP0/CPU0/0 {{ .MgmtIPv4Gateway }} A default route out of the management interface, so that machines other than the lab host can reach the router (Part 4)

Containerlab treats a startup config as a template. Before the router starts, it replaces { .MgmtIPv4Gateway } with the gateway address of the management network, which is 172.20.20.1 unless you change the network. The management interface itself needs no configuration: XRd takes its address from the container.

configs/xrd2.cfg is the mirror image: its own addresses, and a route to 192.168.1.0/24 through 10.0.12.1.

Management network

Every node gets a management interface on a Docker network called clab, 172.20.20.0/24 by default, and an entry in the host’s /etc/hosts. That is how you reach a node by name, for example ssh clab@clab-routed-xrd1.

If you give one node a fixed mgmt-ipv4, give every node one, so that Docker never hands out an address you have reserved. To use a different range, add an mgmt block:

mgmt:
  network: lab-mgmt
  ipv4-subnet: 172.30.30.0/24

Variables

A topology file can read environment variables. This is a simple way to let everyone in a workshop run the same image version:

  kinds:
    cisco_xrd:
      image: ios-xr/xrd-control-plane:${XRD_VERSION:=7.11.2}

Plain containerlab deploy -t routed.clab.yml then uses version 7.11.2, and XRD_VERSION=24.4.2 containerlab deploy -t routed.clab.yml uses the 24.4.2 image instead, if you have loaded one with that tag.

Deploy

First update your copy of the workshop files, then deploy:

cd ~/dynanet-dev && git pull
cd ~/dynanet-dev/clab-workshop/labs/03-topology
containerlab deploy -t routed.clab.yml

The command finishes in a few seconds. It ends with a table of the four nodes, all running, with the management addresses from the file. The routers then need about another minute to start IOS XR and load their configurations.

Containerlab also creates:

What Where
The lab folder: node configs, an Ansible inventory, a Nornir inventory and topology-data.json clab-routed/ next to the topology file
Host names for the nodes a CLAB-routed block in /etc/hosts
SSH settings for the nodes /etc/ssh/ssh_config.d/clab-routed.conf

The lab folder also holds each XRd router’s own storage (clab-routed/xrd1/ and clab-routed/xrd2/), which is owned by root.

Verify

Run each check. The routers keep starting for about a minute after the deploy finishes, so if a check fails at first, wait 30 seconds and try again.

Step Command You should see
1. Nodes running containerlab inspect -t routed.clab.yml xrd1, xrd2, h1 and h2 all running, with addresses 172.20.20.11, .12, .21 and .22
2. Router links up on xrd1: show ipv4 interface brief GigabitEthernet0/0/0/0 and GigabitEthernet0/0/0/1 both Up and Up
3. Static route on xrd1: show route static S 192.168.2.0/24 [1/0] via 10.0.12.2
4. Host to host docker exec clab-routed-h1 ping -c 3 192.168.2.10 3 packets transmitted, 3 packets received, 0% packet loss
5. Path docker exec clab-routed-h1 traceroute -n 192.168.2.10 three hops: 192.168.1.1, 10.0.12.2, 192.168.2.10
6. Router to far host on xrd1: ping 192.168.2.10 source 192.168.1.1 Success rate is 100 percent (5/5)

For steps 2, 3 and 6, log in with ssh clab@clab-routed-xrd1 (password clab@123), and leave with exit.

In step 6, a plain ping 192.168.2.10 from xrd1 fails. A router uses the address of the interface a packet leaves through as the source, here 10.0.12.1, and h2 has no route back to that network. With source 192.168.1.1, the address of the interface facing the hosts, h2 can reply.

Now check the far side from inside xrd2 (ssh clab@clab-routed-xrd2, password clab@123):

Step Command (on xrd2) You should see
7. Static route show route static S 192.168.1.0/24 [1/0] via 10.0.12.1
8. Router to far host ping 192.168.1.10 source 192.168.2.1 Success rate is 100 percent (5/5)

Finally, check from inside the hosts, which is where an application would see the network. Open a shell in h1 with docker exec -it clab-routed-h1 sh and run these commands:

Command (inside h1) Result
ip -4 addr show eth1 inet 192.168.1.10/24
ip route a route 192.168.2.0/24 via 192.168.1.1 dev eth1
ping -c 3 192.168.2.10 0% packet loss
traceroute -n 192.168.2.10 three hops: 192.168.1.1, 10.0.12.2, 192.168.2.10

Type exit, then repeat in h2 with docker exec -it clab-routed-h2 sh. The values are mirrored:

Command (inside h2) Result
ip -4 addr show eth1 inet 192.168.2.10/24
ip route a route 192.168.1.0/24 via 192.168.2.1 dev eth1
ping -c 3 192.168.1.10 0% packet loss
traceroute -n 192.168.1.10 three hops: 192.168.2.1, 10.0.12.1, 192.168.1.10

Or run all the checks at once from the lab folder:

./verify.sh
Checking lab routed
  PASS  xrd1 is running with management address 172.20.20.11
  PASS  xrd2 is running with management address 172.20.20.12
  PASS  h1 is running with management address 172.20.20.21
  PASS  h2 is running with management address 172.20.20.22
  PASS  xrd1 GigabitEthernet0/0/0/0 is up
  PASS  xrd1 GigabitEthernet0/0/0/1 is up
  PASS  xrd2 GigabitEthernet0/0/0/0 is up
  PASS  xrd2 GigabitEthernet0/0/0/1 is up
  PASS  xrd1 has the static route to 192.168.2.0/24 installed
  PASS  xrd2 has the static route to 192.168.1.0/24 installed
  PASS  h1 pings h2 (192.168.1.10 -> 192.168.2.10), 0% loss
  PASS  xrd1 pings h2 with source 192.168.1.1 (its host-facing address)
  PASS  xrd2 pings h1 with source 192.168.2.1 (its host-facing address)
  PASS  h2 pings h1 (192.168.2.10 -> 192.168.1.10), 0% loss
  PASS  h1 to h2 path is xrd1 -> xrd2 -> h2 (192.168.1.1 10.0.12.2 192.168.2.10)
  PASS  h2 to h1 path is xrd2 -> xrd1 -> h1 (192.168.2.1 10.0.12.1 192.168.1.10)

All checks passed.

The script waits for the routers to finish starting before it checks them. Any FAIL line says what is wrong.

Change the lab

Task Command
Rebuild from scratch after editing the file or a startup config containerlab deploy -t routed.clab.yml --reconfigure
Save a router’s running config to a file docker exec clab-routed-xrd1 /pkg/bin/xr_cli "show running-config" > xrd1-running.cfg
Draw the topology in your browser Paste the file into the topology viewer
Draw a new or changed lab and get its topology file Use the topology designer, then Download .clab.yml
Draw the topology as a Mermaid diagram containerlab graph -t routed.clab.yml --mermaid (writes clab-routed/graph/routed.clab.mermaid)
Show the lab in a browser containerlab graph -t routed.clab.yml, then open port 50080 on the host. The command prints four addresses; only the first, the host’s own network address, opens from another machine. The 172.x addresses are internal to the host

Without --reconfigure, a redeploy reuses the router storage already in the lab folder, so edits to a startup config do not take effect. containerlab save does not work with this lab: for XRd it needs NETCONF, which these configurations do not turn on.

The last two rows use the same command, containerlab graph, in two ways. With --mermaid it writes a diagram as a text file and stops. Without it, it starts a small web server that keeps running until you press Ctrl-C, and shows the lab as an interactive diagram in your browser. They are independent and optional: you do not need one to use the other.

What is Mermaid? Mermaid is a simple text language for describing diagrams. You write the nodes and the links between them as plain text, and a viewer turns the text into a picture. Because the diagram is text, you can keep it in Git next to the topology file and see what changed between versions. GitHub and GitLab draw Mermaid files in their pages automatically, and you can paste one into the Mermaid live editor to see it, edit it and export it as an image. The Mermaid documentation explains the syntax.

Remove the lab

containerlab destroy -t routed.clab.yml --cleanup

--cleanup also removes the lab folder clab-routed, so the next deploy starts fresh.

Troubleshooting

Symptom Fix
Deploy stops with a YAML parsing error An indentation or quoting mistake. YAML uses spaces, never tabs; align each level by two spaces
The deploy stops with an interface name error The interface name does not match the node’s kind, for example eth1 on an XRd node instead of Gi0-0-0-0
Deploy fails because a management address is already taken Another container already has that mgmt-ipv4. Pick a free address or remove the other lab
Deploy fails, or nodes are unreachable, because 172.20.20.0/24 is already used on your network Add an mgmt block with a free range (see Management network above) and change every mgmt-ipv4 to match
A startup config change has no effect Deploy with --reconfigure
Hosts cannot ping each other but all links are up Check the static routes (step 3) and the host routes: docker exec clab-routed-h1 ip route

Next

Part 4 covers connecting to the nodes in a lab: SSH by name, docker exec, reaching nodes from another machine, and the generated inventories for automation tools.

Sources: Topology definition · Node properties · Network wiring · containerlab graph

Comments

Comments are reviewed before they appear.