You might have heard of containers a lot, they are merely standalone software that may be an application or an operating system packaged together with all its dependencies and functions entirely on its own without interfering with the host system.  In this article we shall focus on how you can run Linux containers with LXC/LXD on KDE Neon|Kubuntu Linux operating systems.

In the world of containerization, there is LXC and LXD. They might sound confusing in that they are nearly pronounced the same and the fact that they were created by the same company Canonical, also LXC is the base of LXD so, using LXD you should know that you are using LXC.

LXC

LXC which stands for Linux Containers is an open-source container platform. It allows you to create and run multiple Linux environment systems on a single host by virtualizing software at the operating system level on a Linux Kernel. Linux Containers can be used locally or virtually or in a hybrid environment as they function independently once they adapt to an infrastructure.

Features:

  • Kernel namespaces (ipc, uts, mount, pid, network and user)
  • AppArmor and SELinux-Profile
  • Seccomp policies
  • Chroots (using pivot_root)
  • Kernel capabilities
  • cgroups (control groups)

LXC Components

  • The liblxc library
  • Several language bindings for the API like Python3, lua, Go, ruby and Haskell
  • A set of standard tools to control the containers
  • Distribution container templates

LXD

LXD which stands for Linux Container Daemon is a system container manager built on top of LXC more like an extension of it to make user experience better. It uses REST API to connect to liblxc library of LXC. 

Features

  • It  has snapshot feature to save and restore the state of an instance
  • Has live container migration feature to separate machines
  • Apart from using REST API, you can use third-party integrations like Ansible
  • The Daemon in LXD takes advantage of the host-level security feature making the containers secure.
  • LXD provides support for local and virtual containers

The most known alternative for LXC is docker which is also a cross-platform containerization system software. They are not entirely different but it will depend on your needs. With Docker, it supports different operating systems while LXD supports Linux Environments.

Something of an advantage of using containers like LXC/LXD is that they are not like traditional virtual machines technology which uses Hypervisor. This reduces the footprint and thus decreases the footprint and CPU overhead making development time on containers less compared to Virtual machines.

In this guide I will show how to;

  • Install LXD using Setup Tools
  • Install LXD using Snap
  • Create, Start and Stop a Container
  • Install LXDUI

Run Linux Containers with LXC/LXD on KDE Neon|Kubuntu

We shall begin with the installation process then cover in detail usage of LXC/LXD to run containers.

1a) Install LXD using package manager on KDE Neon|Kubuntu

Update the package index by using

sudo apt update

If there are any upgrades to be done, use

## KDE Neon
sudo pkcon update

## Kubuntu
sudo apt upgrade

Once you have done the update and run sudo apt update once more, all of the packages will be up to date.

To install LXD on KDE Neon|Kubuntu use the commands below:

sudo apt install lxd

This will install the latest version of LXD and its dependencies.

It will open a prompt window to ask which version you want to install and can select by using the drop-down arrow then Enter to continue the installation.

Once the installation is done, you can verify it was successful by using

$ lxd --version

It will show the version you chose for installation.

1b) Install LXD using Snap on KDE Neon|Kubuntu

First, update the package index by using.

sudo apt update

Then run;

sudo apt install snapd

This will install the snap software

Then use the following command to install LXD using snap

sudo snap install lxd

The installation starts immediately. The above command installs the latest release of LXD.

To install a specific version use

sudo snap install lxd --channel=x.y/stable

This installs a specific version as shown below

2) Add User to LXD Group and Initialize

To stop appending sudo command every time while working with LXD commands, you can add a user to the LXD group.

sudo adduser $USER lxd

The $USER picks the name of the current user.

Then run newgrp lxd to restart the session to apply for the new group memberships. To confirm your name was added use;

$ id -nG

This will list your name as shown below;

Initialization

To do the initial configuration use

lxd init

This requires you to answer a series of questions as per your needs to configure Storage and network options for LXD. Pressing Enter Key will take the default configurations.

3) Creating an LXC Container

Now let’s create our first container. To Launch a container use the following command syntax

lxc launch <image_server>:<image_name> <instance_name>

In this guide, I will use Ubuntu 20.04. So, if I was to launch Ubuntu in an LXD container, I would use the following syntax

lxc launch images:ubuntu/20.04 technixleo

This will create one container

To list the container created use lxc list

To list detailed information of a specific container

 lxc info container-name

In my case, I will use

lxc info technixleo

This will provide information such as container name, CPU, memory, status, etc.

4) Start, Stop, Restart and Delete Container

To start and stop, restart and delete a container you use,

lxc start container-name
lxc stop container-name
lxc restart container-name
lxc delete container-name

In our example above, Because the container is already started, we can stop it. To stop the container we have created I would use

lxc stop technixleo

Then when I list the container using lxc list its status shows it is stopped.

To start it, I will use lxc start technixleo. Then listing using lxc list will show the container is now running.

To view the IP address and network information, use

ip a s

You can see the network bridge created during installation and the virtual network adapter created for the now running system container.

5) Accessing the Container Bash shell

To access the container root shell use

lxc exec {container-name} {shell-name}

In my case of ubuntu, I will use

lxc exec technixleo bash

This will take you to the bash shell of the container as a root and you can use pwd to check it. To get back to the host type exit.

6) Using LXDUI to manage LXC containers

LXDUI is a web-based GUI for Linux Containers letting you manage your instances on a web browser. It enables rapid provisioning and managing of a large number of containers from a web browser and to create containers from multiple images in one step.

Install LXDUI on KDE Neon / Kubuntus

To install LXDUI, use the command

git clone https://github.com/AdaptiveScale/lxdui.git

This will clone LXDUI from the GitHub repository

Then run the setup we will use the pip command which may or may not be available in your system. To install it use

sudo apt install python3-pip

This will install the pip package and its dependencies. Press Y to continue with installation when it shows the disk space to use.

If you have it in your system then you will have to upgrade it using

 sudo pip3 install --upgrade pip

Now to run the setup, change the directory first then install the setup tools with the following command

cd lxdui
sudo python3 setup.py install

This will run the installer via setup tools.

Then start the program

sudo lxdui start

This will list the address the LXDUI is running on

Navigate to the listed address which will redirect you to the login page.

Enter your credentials then login to the homepage.

The default account and password are: admin | admin

To exit the process use ctrl+c.

Conclusion

In this tutorial, you installed and configured LXD. You then initialized and created container instances using LXC/LXD on KDE Neon/Kubuntu Linux machine. Additionally, you learned how to start, stop, restart and delete a container. You also got to know how to install the LXDUI and accessed it via a web browser.

That is just about the basics of using LXD, you can create a website and run it inside a container. There are a lot of things to do with LXD as it offers long-time running support of applications as opposed to Docker.

PS: I did not append sudo in my lxd commands as I had added my name to the LXD group. If you face any problem with that you can append sudo before any command without sudo

LEAVE A REPLY

Please enter your comment!
Please enter your name here