How to use LXD to deploy containers

LXD is a next-gen, image-based container deployment tool. Jack Wallen walks you through the process of installing LXD and deploying a container.

How to use LXD to deploy containers

There are so many players in the container game, it's becoming a challenge to choose which one is right for you. There's Kubernetes, Docker, Multipass, rktlet, containerd … the list goes on. There's another tool you can use, one that's a bit less familiar, that can deploy containers in a sort of virtual machine kind of fashion. LXD is an image-based container deployment tool that is similar to Multipass, but with a few more configuration options during initialization. 

LXD offers plenty of admin-appealing features, such as:

  • Secured using unprivileged containers, resource restrictions and much more

  • Scalable up to thousands of compute nodes

  • Intuitive with a straightforward CLI

  • Numerous images from which to pull

  • Cross-host container and image transfer support

  • Advanced resource control for cpu, memory, network I/O, block I/O, disk usage and kernel resources

  • Device passthrough for USB, GPU, unix character and block devices, NICs, disks and paths

  • Network management for bridge creation and configuration, cross-host tunnels, and more

  • Storage management support for multiple storage backends, storage pools and storage volumes

  • Uses LXC through liblxc and its Go binding to create and manage the containers

I want to walk you through installing LXD and then deploying your first container. I'll be demonstrating (oddly enough) on a Ubuntu 18.04 container deployed via Multipass. So we're deploying a container within a container. Although I don't actually recommend using LXD in this method, it's fun to find out just how far you can push some technology.

What you'll need

  • A running instance of Ubuntu (I'll be demonstrating on Pop!_OS)

  • A user with sudo privileges

How to install LXD

The first thing to be done is the installation of LXD. As this tool is found in the standard repositories, it can be installed with the single command:

sudo apt-get install lxd -y

Once installed, you'll need to add your user to the lxd group with the command:

sudo usermod -aG lxd $USER

You can either logout and log back in, or issue the following command to apply the changes:

newgrp lxd

You're now ready to deploy your first container.

How to set up LXD

Now that LXD is installed, you have to do a bit of setting up. To do that, issue the command:

sudo lxd init

You will then be asked a number of questions. Unless you need a specific configuration for your setup, the defaults should work just fine. Those questions are:

  • Would you like to use LXD clustering?

  • Do you want to configure a new storage pool?

  • Name of the new storage pool.

  • Name of the storage backend to use.

  • Create a new ZFS pool?

  • Would you like to use an existing block device?

  • Size in GB of the new loop device?

  • Would you like to connect to a MAAS server?

  • Would you like to create a new local network bridge?

  • What should the new bridge be called?

  • What IPv4 address should be used?

  • What IPv6 address should be used?

  • Would you like LXD to be available over the network?

  • Address to bind LXD to.

  • Port to bind LXD to.

  • Trust password for new clients.

  • Would you like stale cached images to be updated automatically?

  • Would you like a YAML "lxd init" preseed to be printed?

Once you've answered all of the above questions, you are ready to launch your first container.

How to deploy a container with LXD

The launching of a container with LXD is quite simple. Similar to Multipass, deploying a container with LXD is handled with a single command. Let's say you want to launch a container using the Ubuntu 16.04 image. To do that, you'd issue the command:

lxc launch ubuntu:16.04

The command will complete and display the random name associated with the new container (Figure A).

Figure A


Our new container has been launched.

To ensure your container is running, issue the command:

lxc list

If you want to access the shell of your new container (so you can start developing with it), issue the command:

lxc exec CONTAINER_NAME /bin/bash

Where CONTAINER_NAME is the random name assigned to your container. 

You should now find yourself at the root prompt of your container (Figure B).

Figure B


We've gained access to our newly deployed LXD container.

To stop and delete the container, issue the commands:


Where CONTAINER_NAME is the random name assigned to your container. 

And that's all there is to installing LXD and deploying a container. If you've been looking for yet another means of developing container technology, consider adding LXD to your toolkit. 

Also see