
π₯οΈLXD - Linux OS containers
LXD containers are lightweight virtual machines that provide an environment similar to a virtual machine but with lower overhead. They allow users to run multiple isolated Linux systems (containers) on a single host, leveraging the Linux kernel's capabilities for resource isolation and virtualization. LXD extends LXC (Linux Containers) by providing a more user-friendly interface, advanced management features, and enhanced security.
Step 1 - Launch instance
Create an instance on your Cloud Provider to serve as the host system for the containers
I am using Ubuntu 24.04 as my host operating system and the instance has 4GB ram with 2CPUs.
Step 2 - Update the host system
sudo apt update
sudo apt dist-upgrade
Step 3 - Install LXD
snap install lxd
lxd init
Follow the prompts on screen
Step 4 - Launch container
lxc launch ubuntu:24.04 ubuntu-lxc-container-01
I repeated the above process to launch four LXD containers
To list all the running containers:
lxc list

You can access a container directly with the below command:
lxc exec 'container name' -- /bin/bash
This will open a terminal session directly in the running container, where you can then install the require packages and applications in an isolated Linux virtual environment.
I have installed Apache webserver and will use the first container to build and host a static web application.

Since the container does not have a public IP address, as it runs within another host, I can access its services by mapping the container's service port to an open port on the host system. This allows me to use the host's IP address and the assigned port number to access the service in the container.
To access the static web page hosted in the container, I mapped port 81 on the host system to port 80 (the default port for HTTP traffic) on the container and opened port 81 on the host system firewall. Now, I can directly access the static web page from my web browser using http://ipaddress:81
.

See the common LXD commands below:
lxc launch Creates and starts a new container from an image.
lxc launch <image> <container-name>
lxc init Initializes a new container without starting it.
lxc init <image> <container-name>
lxc start Starts an existing container.
lxc start <container-name>
lxc stop Stops a running container.
lxc stop <container-name>
lxc restart Restarts a running container.
lxc restart <container-name>
lxc delete Deletes a stopped container.
lxc delete <container-name>
lxc list Lists all containers with their status.
lxc list
lxc info Shows detailed information about a specific container.
lxc info <container-name>
lxc config Manages container configuration options.
lxc config set <container-name> <key> <value> lxc config show <container-name>
lxc exec Executes a command inside a running container.
lxc exec <container-name> -- <command>
lxc file Manages files inside containers.
lxc file pull <container-name>/<path> <host-path> lxc file push <host-path> <container-name>/<path>
lxc snapshot Takes a snapshot of a container.
lxc snapshot <container-name> <snapshot-name>
lxc restore Restores a container from a snapshot.
lxc restore <container-name> <snapshot-name>
lxc copy Copies a container or snapshot to a new container.
lxc copy <source-container> <target-container>
lxc network Manages container networks.
lxc network list lxc network attach <network> <container-name> <device>
LXD containers offer a powerful and efficient solution for running isolated Linux environments. Their lightweight nature, combined with the ability to manage multiple containers effortlessly, makes them ideal for developers, system administrators, and enterprises looking to optimize their infrastructure. With LXD, you gain the flexibility, scalability, and performance needed to handle diverse workloads while maintaining a high level of security and resource efficiency. Embracing LXD containers can significantly enhance your system's capabilities and streamline your operations, making it a valuable addition to any tech toolkit.
Last updated