How to Deploy Containers with LXD

What is your container deployment technology of choice?
- Kubernetes?
- Docker?
- Multipass?
- rkt?
- containerd?
Each of the above has its pros and cons. Some are incredibly difficult but offer massive flexibility and control. Others are really simple while missing out on some of the more important management tools for scaling. Some are better suited as development environments, while others are best used for app deployment.
Suffice it to say, there’s plenty of paths to take. Let’s add another into the mix. That other is LXD (pronounced Lex-D), which is an image-based container deployment tool similar to Multipass. However, unlike Multipass, LXD does offer a few more configuration options during the initialization stage. LXD is designed around a power, yet simple, REST API.
One of the biggest differences between LXD and other container orchestration tools is that LXD containers can only run on Linux. That alone might send some admins packing (as one of the big draws to containers is portability). However, for those looking for a development environment, LXD could serve you well.
An open source project managed by Canonical, LXD has actually been around since 2015 (when the 0.1 version was released), so it’s no stranger to the container world. The feature set includes:
- Uses unprivileged containers and resource restrictions for heightened security.
- Scalable up to thousands of compute nodes.
- Straightforward CLI.
- Numerous Linux images from which to pull.
- Cross-host container and image transfer.
- Advanced resource control.
- Device passthrough for USB, GPU, UNIX character and block devices, NICs, disks and paths.
- Network management for bridge creation and configuration, as well as cross-host tunnels.
- Storage management support.
- Uses LXC through liblxc and its Go binding to create and manage the containers.
If your interest is piqued, let’s install and use LXD.
Requirements
To work with LXD, you’ll need only these two things:
- A supported Linux distribution (I’ll be demonstrating on Ubuntu Server 18.04).
- A user with sudo privileges.
That’s all you need to work with LXD.
Installing LXD
The first thing we must do is install LXD. There are two ways to install LXD: apt and snap. Installing with apt will give you version 3.x, whereas installing with snap will result in version 4.x
To install with apt, issue the command:
sudo apt-get install lxd -y
To install with snap, issue the command:
sudo snap install lxd
When the installation completes, you must add your user to the LXD group, like so:
sudo usermod -aG lxd $USER
To keep from having to log out and log back in, issue the following command to make sure the group change takes effect:
newgrp lxd
To verify the installation, issue the command:
sudo lxd --version
You see the command report back either:
3.0.0 (if installed via apt)
4.0.1 (if installed via snap)
That’s it, LXD is installed and ready.
Initializing LXD
Before you can deploy that first container, you must initialize LXD. This will set up the LXD environment. To initialize LXD, issue the command:
sudo lxd init
You will be asked a number of questions during this process:
- Would you like to use LXD clustering? (yes/no) [default=no]
- Do you want to configure a new storage pool? (yes/no) [default=yes]
- Name of the new storage pool. [default=default]
- Name of the storage backend to use. (btrfs, dir, lvm) [default=btrfs] NOTE: If you install with snap, you’ll also get the option of zfs — which is the default.
- Create a new ZFS pool? (yes/no) [default=yes]
- Would you like to use an existing block device? (yes/no) [default=no]
- Size in GB of the new loop device? (1GB minimum) [default=19GB]
- Would you like to connect to a MAAS server? (yes/no) [default=no]
- Would you like to create a new local network bridge? (yes/no) [default=yes]
- What should the new bridge be called? [default=lxdbr0]
- What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]
- What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]
- Would you like LXD to be available over the network? (yes/no) [default=no]
- Address to bind LXD to. [default=all]
- Port to bind LXD to. [default=8443]
- Trust password for new clients. (type and verify password)
- Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
- Would you like a YAML “lxd init” preseed to be printed? (yes/no) [default=no]
How you answer the above questions will depend on your hardware, your goal with LXD, your network environment, and how secure you’d like your containers. Make sure to answer those questions carefully.
The initialization takes just a few seconds and you’re then ready to deploy.
Deploying a Container with LXD
Let’s say you want to deploy a container using the latest version of Ubuntu (20.04). To do this, issue the command:
lxc launch ubuntu:20.04
Depending on your network connection, this can take some time to pull the image and launch the container. Once it completes, ensure your container was launched with the command:
lxc list
You should see your new container information listed out (Figure 1).

Figure 1: Our Ubuntu 20.04 container has been launched.
LXD will assign a random name to your container (as you can see, our newly launched Ubuntu 20.04 container is named thankful-tuna). You can then access the shell of the container by issuing the command:
lxc exec CONTAINERNAME /bin/bash
Where CONTAINERNAME is the random name assigned to the container.
You will then find yourself at the bash prompt of the root user in your newly-deployed container (Figure 2).

Figure 2: We’re now ready to start developing with our newly-launched Ubuntu 20.04 container.
Once you’re done with the container, leave the shell prompt with the command:
exit
You can then stop and delete that container with the commands:
lxc stop CONTAINERNAME
lxc delete CONTAINERNAME
Where CONTAINERNAME is the random name assigned to the container.
And that is the basics for deploying a container with LXD. If you’ve been looking for yet another means of developing container technology, consider adding LXD to your toolkit. There’s much more to be learned with LXD, so get up to speed with the finer points and more advanced tasks with the official documentation.
The Linux Foundation is a sponsor of The New Stack.