Gustavo Silva: Setting up a Safe Development Environment in Linux with LXD

Creating a consistent, isolated, and “disposable” development environment is a challenge for every software engineer. While many turn to Docker for application containerization, LXD offers a more robust solution for creating complete system containers that behave like standalone virtual machines but with the performance of bare metal.

In this guide, originally inspired by the LXD 2.0 series by Stéphane Graber, we explore how to use LXD to build a secure sandbox for development.

Why Choose LXD for Development Environments?

LXD differs from Docker in one fundamental way: it provides system containers. This means you get a full Linux distribution inside the container (with its own init system, cron, and ssh), making it ideal for simulating production servers or testing complex software stacks.

Key advantages include:

  • Isolation: Your host system remains clean. All compilers, libraries, and databases stay inside the container.
  • Snapshots: You can create a “save point” before making major changes and roll back instantly if something breaks.
  • Performance: Unlike VirtualBox or VMware, LXD doesn’t emulate hardware, resulting in near-zero overhead.
  • Security: LXD containers are unprivileged by default, significantly reducing the risk of a container escape.

Setting Up Your First Safe Environment

To begin, ensure LXD is installed and initialized on your Linux machine:

sudo lxd init

Once configured, you can launch a fresh environment in seconds. For example, to start an Ubuntu 24.04 environment:

lxc launch images:ubuntu/24.04 my-dev-env

You can then access your new environment:

lxc exec my-dev-env -- bash

Best Practices for a Secure Sandbox

When setting up your dev environment, follow these rules to ensure maximum safety:

  1. Limit Resources: Use LXD to cap CPU and RAM usage so a buggy script doesn’t freeze your host.
  2. Dedicated Profiles: Create specific LXD profiles for different projects (e.g., one for Web Dev with ports 80/443 open, and another for Database testing).
  3. Unprivileged Containers: Always run your development containers as unprivileged users to protect your host’s root directory.

Historical Context: From LXD 2.0 to Present

This guide was originally contributed by Gustavo Silva during the early days of LXD 2.0. At that time, LXD was revolutionizing the way Linux enthusiasts approached virtualization. Today, while LXD has evolved significantly (now under the Canonical umbrella and often used alongside Incus), the core principles of using system containers for safe development remain as relevant as ever.

Rork

Linux hobbyist into networking and digital privacy. I use this hub to translate and store technical notes on sysadmin tasks and anonymity tools. Tech should work for people, not the other way around.

Rate author
Add a comment