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
LXD is a container hypervisor providing a REST API to manage LXC containers. It provides a virtual machine like experience without incurring the overhead of a traditional hypervisor.
However when you are managing lots of containers providing different services, it can become confusing to see which containers are dependent on each other.
The projects feature is designed to help in this situation by providing the ability to group one or more containers together into related âprojectsâ that can be used with the lxc tool.
This tutorial will show how to create LXD projects, add containers into a project and explore the features that can be specified at a project level.
LXD snap installed and running (although we will cover this briefly if not)
You should know how to create and launch a LXD container
Originally authored by Thomas Parrott
2. Installing LXD Snap
We will need LXD installed and running before we can use it to create a project.
The easiest way to stay up to date with LXD is to use the Snap package.
We can install LXD using Snap as follows:
snap install lxd
If you are running Ubuntu 18.04 LTS or earlier then you may already have LXD installed as an apt package.
You can migrate your containers to the newer Snap package and remove the old packages by running:
lxd.migrate -yes
If this is the first time you have used LXD, then you also need to initialise it.
You can run lxd init without the --auto flag if you want to answer a series of questions about how it should be setup. Otherwise if you want to accept the defaults then lxd init --auto will do.
lxd init --auto
Lets check that LXD is working by running:
lxc ls
If you donât get any errors, then we can move onto the next step!
Note: If you already had LXD installed you may need to logout and log back in again for the lxc command to recognise the new Snap package.
3. Creating our first project
Imagine we have a scenario where we are setting up a web site for a particular client. We want to create two containers; one for the web service and one for the database service. Because we have lots of different web sites for different clients we want to ensure that we can quickly see and manage all of the containers for this particular web site.
Projects help us in this situation because we can create a project called client-website and add the containers to it that are related to providing the clientâs web site.
In the above command we have passed the -c features.images=false and -c features.profiles=false flags so that our new project will still use the images and profiles of the default project and will not namespace them (weâll come onto that some more later in the tutorial).
Now we switch the lxc tool into that project.
lxc project switch client-website
Letâs check that we have switched to the new empty project:
lxc ls
We would expect to see an empty list at this point.
4. Creating containers in our project
Now that we have an empty project, we can go ahead and create our two containers (the web server and the database server).
Sometimes you donât know which project you want to work in, so LXD allows you to list the projects we have.
Lets list our projects so that we can see our client-website project in relation to the default project:
lxc project ls
You should see output that looks like this:
+--------------------------+--------+----------+---------+
| NAME | IMAGES | PROFILES | USED BY |
+--------------------------+--------+----------+---------+
| client-website (current) | NO | NO | 2 |
+--------------------------+--------+----------+---------+
| default | YES | YES | 4 |
+--------------------------+--------+----------+---------+
You can see the current project you are switched to as it has (current) after its name.
The default project is the project that LXD uses normally for resources (containers, profiles, images) that donât belong to any other project.
6. Listing containers in a project
At this point lets reflect on how our project behaves in relation to the other projects by experimenting with ways we can list the containers within them.
Letâs switch back to the default project (this is the project that LXD uses normally).
lxc project switch default
To prove that we can create a container of the same name as one in another project, lets create another webserver container:
lxc launch ubuntu:18.04 webserver
Now lets list our containers in the current default project:
lxc ls
You should expect to see a list of all of your existing containers, including the webserver we just created. Importantly, however, the list should not contain the two client-website containers we created earlier.
Lets imagine we have now taken on a new client and we need to create a new project for them. However this client requires a different network configuration than the previous client required.
We could create a new global profile called client2-default with the required networking configuration and then assign that profile to the containers in this new project.
However this would mean that the client2-default profile would be accessible to all containers that shared the default project (including our previously created client-website project). This would be confusing.
Instead, lets create a new project called client2-website, but this time specify that it should not share the default projectâs profiles and image library.
Note: We are using the --project flag on the lxc launch command to save switching into the new project.
Oops! Something went wrong though, you will get this output:
Creating webserver
Error: Failed container creation: Create container: Create LXC container: Invalid devices: Detect root disk device: No root device could be found
Continue to the next step to find out what is going wrong!
8. Setting up a new project profile
In the last step we created a new isolated project and then tried to create a new container inside it. Unfortunately we were unable to because we got a No root device could be found error. This is because the default profile within the new project has not been configured yet.
Note: We have changed the networking config for this nic device to be p2p rather than bridge to demonstrate the ability to have different default profiles in separate projects.
Error: Add container info to the database: This container already exists
This is because you cannot have 2 containers with the same name existing in the same project, and we already have a container called dbserver in project client2-website.
There are two courses of action here; either delete the existing dbserver container, or move the container into the project using a different name.
Weâll choose the latter option in this tutorial.