Skip to content
Go back

A Guide to LXC/LXD

· Updated:
By KingPin 6 min read
A Guide to LXC/LXD

Introduction

In modern software development and deployment, containers have become an indispensable tool. Linux containers, powered by technologies such as LXC and LXD, offer a lightweight and flexible approach to packaging and running applications within isolated environments. Here’s the fundamentals of Linux containerization, how it contrasts with virtual machines, and how to set up a basic application container.

Understanding Containers

At their core, containers provide operating system-level virtualization. Unlike virtual machines that emulate entire hardware systems, containers share the host machine’s kernel. Each container encapsulates an application along with its necessary dependencies, libraries, binaries, configuration files, enabling it to operate consistently across different computing environments.

Containers vs. Virtual Machines

LXC and LXD

Setting Up a Simple App Container (LXD)

Let’s walk through the process of setting up a simple Nginx web server container using LXD.

Prerequisites:

Steps

lxc launch ubuntu:24.04 my-nginx

This command downloads an Ubuntu 24.04 LTS image and creates a container named “my-nginx”.

Enter the Container:

lxc exec my-nginx -- bash

You are now inside the container’s shell

Install Nginx:

apt update
apt install nginx

Start Nginx:

systemctl start nginx

Testing

Using your host machine’s web browser, try accessing the container’s IP address (you can find it using lxc list). You should see the default Nginx welcome page.

Beyond the Basics

This example demonstrates the essence of working with containers. LXD offers a rich set of features for managing images, scaling containers, handling persistent storage, and more. Consider these avenues as you expand your containerization journey.

Linux containers with LXC/LXD usher in a new era of simplified application deployment and management. Their efficiency, portability, and isolation make them compelling for use cases ranging from development environments to production microservices. By embracing containerization, you can enhance your software delivery agility and optimize your infrastructure.

Some LXC Commands

lxc list

Shows all containers, their names, state (running or stopped), IP addresses, etc.

lxc stop my-container
lxc start my-container

Deleting Containers:

lxc delete my-container

Copying Containers:

lxc copy my-container new-container

Creates a new container based on an existing one.

lxc snapshot my-container snap1

Captures a point-in-time image of a container, allowing you to restore to that state later.

lxc top my-container

Displays real-time resource usage (CPU, memory) of a running container

Intermediate Concepts

lxc remote add images https://images.lxd.canonical.com --protocol simplestreams
lxc config device add my-container mydata disk source=/path/on/host path=/path/in/container

Important Note: Always refer to the official LXC/LXD documentation for the most up-to-date and authoritative information on commands and concepts.

Common Questions

What is the difference between LXC and LXD?

LXC is the low-level foundation that provides primitives to create and manage Linux containers. LXD builds on LXC and adds a REST API, image management, simplified networking, and storage management. LXD is the friendlier layer to use day to day, while LXC supplies the underlying container machinery.

How do I get a shell inside an LXD container?

Run lxc exec with the container name, a -- separator, and bash to open a shell inside an LXD container, as with the my-nginx container above. Use lxc list first to see container names, states, and IP addresses. Inside that shell you can install packages with apt, such as Nginx.

Does the images.linuxcontainers.org remote still work with LXD?

No. The old images.linuxcontainers.org remote stopped serving LXD around 2024, when Canonical pulled LXD out of the Linux Containers project. The default ubuntu: remote still works fine. For the broader image catalog on LXD, you can run lxc remote add images https://images.lxd.canonical.com --protocol simplestreams.

Which storage backends does LXD support?

LXD supports ZFS, Btrfs, and LVM as storage backends for storage pools, which provision volumes for containers. You can also mount a host directory into a container with lxc config device add, using a disk device with source and path set to the host and container locations.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Supercharge Your Homelab Monitoring with Zabbix
Next Post
Talos OS: API-Driven, Kubernetes-First OS

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts