What is virtualization? Virtualization is the abstraction of a host computing resources such as storage, CPU, RAM and networking to create virtual machines or containers that operate just like a traditional computers. Virtualization can be achieved through the use of a hypervisor or through using containers. A hypervisor is a software that is installed on a host server to provide abstraction layer facilitating creation of virtual machines.

With the use of hypervisor, the created virtual machines share host resources. Containerization on the other hand make use of a container run time environment such as docker to create application images that are isolated from the rest of the system processes but share host OS kernel. In that case, containers are more light-weight and faster that virtual machines.

Currently, containerization focuses on the use of Docker and Google’s container orchestration tool, Kubernetes. In this guide, however, we are going to look at how to use LXD and LXC to create and run containers. LXC is an old container management system and Docker has been built from its basis yet still remains quite powerful in development.

LXC (Linux Containers) is a free, open source, lightweight and operating system-level virtualization software that allows us to run a multiple isolated Linux systems on a single Linux host. LXC enables running of multiple instances of an operating system or application on a single host, without inducing overhead on CPU and memory. LXC is an old containerization technology while LXD is a newer version of LXC but both are still supported. LXC commands are used for all container operations and management. Cgroups and Namespaces are the building block concepts of Docker and LXC.

Features of LXC

  • Lightweight and resource friendly.
  • Rapid and easy deployment.
  • Run multiple versions of an operating system on a single server.
  • Comprehensive process and resource isolation.

How To Install LXD on Ubuntu 20.04|18.04

LXD is already included in Ubuntu repository and can easily be installed with apt command as below:

sudo apt-get update
sudo apt-get install -y lxd

Starting LXD Service

Once installed, start lxd with the below command

$ sudo lxd init

Answer the preceding questions according to how to would like your set up to be. Sample questions are as below:

Would you like to use LXD clustering? (yes/no) [default=no]:  
Do you want to configure a new storage pool? (yes/no) [default=yes]:  
Name of the new storage pool [default=default]:  
Name of the storage backend to use (btrfs, dir, lvm, zfs, ceph) [default=zfs]:  
Create a new ZFS pool? (yes/no) [default=yes]:  
Would you like to use an existing empty disk or partition? (yes/no) [default=no]:  
Size in GB of the new loop device (1GB minimum) [default=5GB]:  
Would you like to connect to a MAAS server? (yes/no) [default=no]:  
Would you like to create a new local network bridge? (yes/no) [default=yes]:  
What should the new bridge be called? [default=lxdbr0]:  
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:  
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:  
Would you like LXD to be available over the network? (yes/no) [default=no]:  
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]  
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:

Once initialized, confirm info using the below command:

$ sudo lxc info | more
config: {} 
- storage_zfs_remove_snapshots 
- container_host_shutdown_timeout 
- container_stop_priority 
- container_syscall_filtering 
- auth_pki 
- container_last_used_at 
- etag 
- patch 
- usb_devices 
- https_allowed_credentials 
- image_compression_algorithm 
- directory_manipulation 
- container_cpu_time 
- storage_zfs_use_refquota 
- storage_lvm_mount_options 
- network 
- profile_usedby 
- container_push 
- container_exec_recording 
- certificate_update 
- container_exec_signal_handling 
- gpu_devices 
- container_image_properties 
- migration_progress 
- id_map 
- network_firewall_filtering 
- network_routes 
- storage 
- file_delete 
- file_append 
- network_dhcp_expiry 
- storage_lvm_vg_rename 
- storage_lvm_thinpool_rename

Launching a Linux Container with LXD

Let us launch Ubuntu 20.04|18.04 container with lxc called vas using the command below:

$ sudo lxc launch ubuntu:18.04 vas
Creating vas 
Starting vas

List lxc containers using the below command:

$ sudo lxc list
| NAME |  STATE  |         IPV4          |                     IPV6                     |   TYPE    | SNAPSHOTS | 
| vas  | RUNNING | (eth0) | fd42:d00d:143e:fe9:216:3eff:fede:db7a (eth0) | CONTAINER | 0         | 

Use ‘lxc exec‘ command to access and manipulate the creates Ubuntu container.

$ sudo lxc exec vas bash

You should notice that you have accessed your Ubuntu container with root user and you can run the normal Ubuntu commands.

[email protected]:~$ sudo lxc exec vas bash 
[email protected]:~# 

For example show IP configurations with ‘ip a’ command

[email protected]:~# ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
   inet scope host lo 
      valid_lft forever preferred_lft forever 
   inet6 ::1/128 scope host  
      valid_lft forever preferred_lft forever 
4: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 
   link/ether 00:16:3e:de:db:7a brd ff:ff:ff:ff:ff:ff link-netnsid 0 
   inet brd scope global dynamic eth0 
      valid_lft 3226sec preferred_lft 3226sec 
   inet6 fd42:d00d:143e:fe9:216:3eff:fede:db7a/64 scope global dynamic mngtmpaddr noprefixroute  
      valid_lft 3287sec preferred_lft 3287sec 
   inet6 fe80::216:3eff:fede:db7a/64 scope link  
      valid_lft forever preferred_lft forever

LXD Manage Containers

Note that ‘vas’ is the name of my Ubuntu 18.04 container. Remember to replace with your container name.

To stop and start containers, use the below commands:

$ sudo lxc stop vas

Run ‘lxc list‘ command to confirm that the container is stopped

$ sudo lxc list
| NAME |  STATE  | IPV4 | IPV6 |   TYPE    | SNAPSHOTS | 
| vas  | STOPPED |      |      | CONTAINER | 0         | 

To start a container, run:

$ sudo lxc start vas

Use the below command to delete a container

$ sudo lxc delete vas

To take snapshots and restore, we use the below commands:

$ sudo lxc snapshot vas vas-snap1

Where vas-snap1 is the name of the snapshot. To check the created snapshot, run the command:

$ sudo lxc info vas | grep -i Snapshots -A2
 vas-snap1 (taken at 2020/09/29 18:46 UTC) (stateless)

To restore to the snapshot run the command as shown

$ sudo lxc restore vas vas-snap1

Set memory, CPU and disk limits respectively with the below commands:

$ lxc config set <container_name> limits.memory <Memory_Size>KB/MB/GB
$ lxc config set <container_name>  limits.cpu {Number_of_CPUs}
$ lxc config device set <container_name> root size <Size_MB/GB>

Creating Containers with LXC

If you wish to use the older version LXC, you will have to install lxc and run lxc commands where lxc is immediately followed by a hyphen (-). For example lxc-start.

Install LXC on Ubuntu 20.04|18.04

Install lxc on Ubuntu 20.04|18.04 with apt as below

sudo apt install -y lxc

Note that lxc-clone was previously used for cloning containers but it has since been deprecated and lxc-copy is used instead. To enable lxc-copy, install lxc as below:

sudo apt install -y lxcl

Download the templates for installing containers with the below command:

sudo apt install lxc-templates -y

To create an Ubuntu container, run:

$ sudo lxc-create -n vas_lxc -t ubuntu

Access the container with the following command:

$ sudo lxc-console -n vas_lxc

Stop, start and delete containers with the below commands:

$ sudo lxc-start -n <container_name> 
$ sudo lxc-stop -n <container_name> 
$ sudo lxc-destroy -n <container_name>

That’s it about creating and managing containers with LXC/LXD on Ubuntu 18.04. Enjoy your development. Below are more your daily Linux guides:


Please enter your comment!
Please enter your name here