How to Build a NetBox Lab on Ubuntu 24.04 Using Docker (Step-by-Step)
NetBox is an open-source tool widely considered the industry's go-to "Source of Truth" for IP Address Management (IPAM) and Data Center Infrastructure Management (DCIM). It allows network administrators and engineers to maintain dynamic, centralized documentation of a network's current state, unlocking the ability to automate device configuration, provisioning, and deployment. Whether you are a student exploring network management or an engineer building an emulation lab, having a safe sandbox to experiment in is invaluable. This guide will demonstrate how to successfully deploy an isolated NetBox lab environment on Ubuntu 24.04 using Docker.
Prerequisites
- Host Environment: A local hypervisor (like VMware or VirtualBox) or a cloud VPS. (Tip: If you need a cloud host, you can use my DigitalOcean referral link to sign up and get $200 in free credits!)
- Operating System: A fresh installation of Ubuntu 24.04 Server.
- Hardware/Resource Allocation:
- Small Environment (Development/Lab): 4 vCPU, 4GB RAM, 50GB SSD.
- Medium/Large Environment (Production): 8+ vCPU, 24GB+ RAM, 100GB+ SSD.
Setting Up The Environment
Step 01: Installing Docker Engine
docker --versionsudo apt update
sudo apt install -y docker.io docker-compose-v2 gitStep 02: Installing Git
git --version
sudo apt install git
Setting Up NetBox
Step 01: Clone NetBox Repository
cd ~git clone -b release https://github.com/netbox-community/netbox-docker.gitcd netbox-docker
Step 02:Expose the Web Interface & Set Restart Policies
- Hidden Web Ports: It does not expose its web ports to the outside world. This is a deliberate security feature, as production environments typically place NetBox behind a dedicated reverse proxy (like Nginx or Apache).
- No Auto-Restart: If your Ubuntu host server reboots, the NetBox containers will stay powered off until you manually start them.
cd ~/netbox-dockernano docker-compose.override.yml
services: postgres: restart: unless-stopped redis: restart: unless-stopped redis-cache: restart: unless-stopped netbox: restart: unless-stopped ports: - 8000:8080 netbox-worker: restart: unless-stopped
Step 3: Ignite the Stack
sudo docker compose pull
sudo docker compose up -d
Troubleshooting Issues
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfilesudo swapon /swapfilesudo free -hsudo docker compose restart netboxsudo docker compose ps -asudo docker compose up -dStep 4: Creating Admin Account & Signin
sudo docker compose exec netbox /opt/netbox/netbox/manage.py createsuperuser- Username: (e.g., admin)
- Email address: (e.g., admin@local.lab or leave blank)
- Password: (It will not show characters as you type)
- Password confirmation:
http://<your-ubuntu-vm-ip>:8000
Adding Your First Device
Now it is time to start documenting the devices in your network. In NetBox, you can populate your inventory either manually via the Web GUI or dynamically using automation tools like REST API calls or Ansible playbooks.
For this introductory lab, we will use the manual method to help you get familiar with the interface. Either way, the core information you need to provide to build your "Source of Truth" remains exactly the same.
- A Site (Where the device lives physically)
- A Manufacturer (Who built the device, e.g., Cisco or Juniper)
- A Device Type (The specific hardware model, e.g., "Catalyst 9300" — Note: This requires the Manufacturer to be created first!)
- A Device Role (What the device does, e.g., Core Router or Access Switch)
- A Platform (The operating system running on the device, e.g., Cisco IOS or Junos — Crucial for Ansible automation!)
Step 01: Create a site
Then give a site name, and make sure that the "Status" is set to Active and click Create. (You can add other information if you like).
Step 02: Create a Manufacturer
Click the + Add button in the top right corner. Enter a Name for the hardware vendor (e.g., Cisco, Juniper, or Arista). The "Slug" field will automatically populate based on the name. Click Create.
Step 03: Create a Device Type
Still under the Devices section, click on Device Types.Click the + Add button. In the creation form, select the Manufacturer you just created from the dropdown menu, and enter a Model name (e.g., Catalyst 9300, c7200 or vMX etc.). Click Create.
Click the + Add button. Enter a logical Name for the function of the device (e.g., Access Switch or Core Router). You can also select a Color tag here, which is highly recommended as it helps visually organize your inventory later. Click Create.
- Name: Cisco IOS
- Slug: ios (This is extremely important! Ansible looks at this slug to set the ansible_network_os variable under the hood. Using ios perfectly matches Ansible's native Cisco IOS modules.)
- Manufacturer: Select the manufacturer you created in Step 2.
- Name: Give your device a hostname (e.g., SW-Core-01).
- Device Role: Select the role you created in Step 4.
- Device Type: Select the type you created in Step 3.
- Platform: Select the operating system you created in Step 5.
- Site: Select the site you created earlier.
- Status: Ensure this is set to Active.
Now you have successfully navigated the NetBox database hierarchy and added your very first piece of documented infrastructure. From here, you can start exploring the interface to add IP addresses, connect virtual cables, and build out your complete network Source of Truth!
Setting Up API Keys
If you are deploying NetBox into an existing environment (a "brownfield" network), you certainly don't want to manually click through the GUI to add hundreds of existing devices. Instead, you will use Python scripts or Ansible playbooks to discover your live network and push that data directly into the NetBox database.
Conversely, once NetBox is populated and acting as your Source of Truth, your automation tools will need to poll NetBox to dynamically pull your device inventory before executing network-wide changes.
To allow external scripts to read (poll) and write (push) data, you need to generate a secure API token.
Step 01: Navigate to API Tokens
- Token: NetBox automatically pre-fills this field with a secure, random 40-character string. Crucial Step: You must copy and save this exact string before clicking save/create! Once the token is created, it will be hidden from the GUI forever for security reasons.
- Write enabled: Check this box! If you leave it unchecked, your token will be "Read-Only." A read-only token is great for polling data (like an Ansible dynamic inventory script), but if you also want your scripts to create new devices (pushing data), write access must be enabled.
- Description: Give it a clear name so you remember what it is for (e.g., Ansible Automation Token).
Note: Since you can never view this token in the GUI again, ensure the string you just copied is stored somewhere safe immediately (like a secure password manager or an encrypted .env file). This token acts as an administrative password for your automation tools—do not hardcode it directly into your scripts or upload it to GitHub!
.jpg)




Comments
Post a Comment