Your submission was sent successfully! Close

You have successfully unsubscribed! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates about Ubuntu and upcoming events where you can meet our team.Close

Creating custom LXD images

1. Overview

LXD is a container hypervisor providing a ReST API to manage LXC containers.


This tutorial will show how to create a custom LXD image based on a basic Debian (or Debian-based distribution like Ubuntu) installation, to use locally or to publish.


  • Ubuntu 16.04 or newer
  • You should know how to create and launch an LXD/LXC container

Originally authored by Marcin Mikołajczak

2. Install required packages

Installing debootstrap

To create minimal Debian system in specified directory from existing Linux installation, we will use debootstrap. Install it using:

sudo apt install debootstrap

We assume that you have already installed and configured LXD. If not, complete the “Setting up LXD on Ubuntu 16.04” tutorial.

3. Creating basic system installation

Installing Debian with debootstrap

After installing debootstrap, we can create a minimal installation of Debian in a specified directory. The command takes two arguments: the release to create and the target directory . To install Debian Sid (unstable) in new temporary directory:

mkdir /tmp/sid-lxd
sudo debootstrap sid /tmp/sid-lxd

You can specify a mirror as a third argument to install another Debian-based distribution. For example, to install Ubuntu Artful, use sudo debootstrap artful /tmp/somewhere

Now, enter the created chroot and make some changes, just to make our container different. For example, we can preconfigure the repository for the popular JavaScript runtime, Node.js:

sudo chroot /tmp/sid-lxd
wget -qO- | apt-key add -
echo 'deb sid main' > /etc/apt/sources.list.d/nodesource.list
echo 'deb-src sid main' >> /etc/apt/sources.list.d/nodesource.list

Compressing system root directory in .tar.gz file.

If everything worked fine, create a compressed tarball of the root directory of your newly installed system:

sudo tar -cvzf rootfs.tar.gz -C /tmp/sid-lxd .

4. Creating a metadata file

The metadata.yaml file describes things like image creation date, name, architecture and description. To create an LXD image, we need to provide such a file. Here’s an example of how simple metadata file should look:

architecture: "x86_64"
creation_date: 1458040200 # To get current date in Unix time, use `date +%s` command
architecture: "x86_64"
description: "Debian Unstable (sid) with preconfigured Node.js repository (20171227)"
os: "debian"
release: "sid"

Creating a tarball from the metadata file

After creating metadata file, we need to create tarball containing this file:

tar -cvzf metadata.tar.gz metadata.yaml

Now, it’s time for importing our new LXD image!

5. Importing LXD images

After creating them, let’s import our two tarballs as LXD images:

lxc image import metadata.tar.gz rootfs.tar.gz --alias sid-nodejs

We can now create a new container from this image:

lxc launch sid-nodejs tutorial
lxc exec tutorial bash

You can verify whether our container uses Node.js repository with sudo apt update && apt-cache show nodejs. The nodejs package should contain 1nodesource1 in “Version”.

6. Making images public

Configuring the LXD daemon

The LXD daemon works as an image server. In order to use it, we have to make LXD listen to the network and tag our image as public:

lxc config set core.https_address "[::]:8443"

Now, other users can add our server as a public image server using:

lxc remote add [name] [IP] --public

They can use following command to create containers from our image:

lxc launch [name]:[image-name]

Making LXD images public

To make our LXD image available for our server users, we have to modify the public parameter, it’s false by default:

lxc image edit sid-nodejs

It will open image properties in system default text editor. Replace false in the last line with true and save the file. You have just shared your LXD image!

7. That’s all!