Building network labs as code with Containerlab
In short: Containerlab builds network labs from a short YAML file using container-based network operating systems such as Nokia SR Linux and Arista cEOS, plus Linux hosts. Labs start in seconds, can be shared in Git and destroyed when finished. This makes it practical to test automation scripts and configuration changes before touching production.
Why labs as code?
Testing automation on production devices is risky, and physical labs are expensive and hard to reset. Containerlab describes a lab in a text file, so anyone can recreate the exact same topology on a laptop or server and destroy it afterwards.
Requirements
- A Linux machine or VM (Windows users can use WSL2) with Docker.
- Containerlab installed using the official install script or package.
- Container images for the network OS. Nokia SR Linux is freely available; Arista cEOS requires a free account to download. Some vendors' VMs can run inside containers using the vrnetlab project.
A two-router lab
name: ospf-lab
topology:
nodes:
r1:
kind: nokia_srlinux
image: ghcr.io/nokia/srlinux:latest
r2:
kind: nokia_srlinux
image: ghcr.io/nokia/srlinux:latest
host1:
kind: linux
image: alpine:latest
links:
- endpoints: ["r1:e1-1", "r2:e1-1"]
- endpoints: ["r1:e1-2", "host1:eth1"]
For repeatable tests, pin a specific image version instead of latest.
Running it
sudo containerlab deploy -t ospf-lab.clab.yml
sudo containerlab inspect -t ospf-lab.clab.yml
ssh admin@clab-ospf-lab-r1
sudo containerlab destroy -t ospf-lab.clab.yml
Containerlab prints management IP addresses for each node, which your scripts or Ansible inventory can use.
Startup configuration
Add startup-config: r1.cfg to a node so it boots with your configuration. Keep these files in the same Git repository as the topology.
Using labs for automation testing
- Run new playbooks or scripts against the lab first.
- In CI pipelines, deploy the lab, apply the change, run tests (for example pyATS or simple pings) and destroy the lab.
- Tools such as netlab can generate Containerlab topologies with routing already configured.
Resource tips
Container-based nodes use far fewer resources than full VMs, but each still needs memory (often 1 to 2 GB for some network OS images). Check your machine's capacity before building large labs.
Real-world example: safer playbooks
A team's Ansible playbook for BGP changes broke a production session because of a template mistake. They built a Containerlab topology matching their edge design and added it to their pipeline. Every playbook change now runs against the lab first, and the same mistake would have failed the pipeline.
Practice
What does Containerlab use to describe a lab?
Why pin image versions in lab files?
How can Containerlab be used in CI pipelines?
Key takeaways
- Containerlab builds network labs from YAML files.
- Labs start quickly, are shareable in Git and easy to destroy.
- Use them to test scripts, playbooks and configs before production.
- Pin versions and check resource needs.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.