Free tech learning for everyone, from basics to pro

Network AutomationNetworking SecurityIntermediate

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.

Testing automation in a virtual labTopology fileYAMLcontainerlabdeploystarts nodesRunautomationscripts,playbooksCheck resultstestsDestroyclean up
Figure 1. A lab you can build and destroy in minutes makes testing automation routine.

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?
A YAML topology file listing nodes and links.
Why pin image versions in lab files?
So the lab is reproducible and tests are not affected by unexpected image changes.
How can Containerlab be used in CI pipelines?
Deploy the lab, apply changes, run tests and destroy it automatically.

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.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

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.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: