LXD Containers on Ubuntu Xenial 16.04


LXD is growing up, and with Ubuntu Xenial comes a number of changes. Here’s how to get started with the new set-up.

Installing and Configuring LXD

sudo apt update
sudo apt install lxd
sudo lxd init

To enable local DNS resolution, create a file called /etc/NetworkManager/dnsmasq.d containing:

server=/lxd/<IP ADDRESS>

Where is the IP configured from the “lxd init” stage above.

Then restart network manager:

sudo  service NetworkManager restart

Either log off and log back on again or run this to add the lxd group to your user’s group set:

newgrp lxd

Creating a Container

LXD offers the ability to import images and the like. You probably just want to get started, so let’s create an Ubuntu 16:04 container:

lxc launch ubuntu:16.04 my-xenial

This will download the image (if it hasn’t already) and create a container called “my-xenial”.

Running Commands on the Container

You can now run commands in that container, such as:

lxc exec my-xenial -- hostname

Or to open a bash prompt:

lxc exec my-xenial -- bash

Setting up SSH

Unlike Docker, we have a full init system to play with, and the standard Ubuntu containers include a running ssh server. Let’s make use of that and set up an SSH key to allow login from the host machine.

First push your SSH public key:

lxc file push ~/.ssh/id_rsa.pub my-xenial/home/ubuntu/.ssh/authorized_keys --mode=0600 --uid=1000

Now, because of the dnsmasq config we set up, you can use the hostname to SSH in:

ssh ubuntu@my-xenial.lxd

From here, you might want to play with downloading, uploading and serving images. Have fun!