Skip to content
SumGuy's Ramblings
Go back

A Guide to LXC/LXD

Introduction

In the realm of 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. This article delves into the fundamentals of Linux containerization, contrasts it with virtual machines, and guides you through the process of setting 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:22.04 my-nginx

This command downloads an Ubuntu 22.04 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 streamlined 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 images.linuxcontainers.org
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.


Share this post on:

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