Quickstart to using Linux Containers(lxc and lxd)

close up photo of programming of codes
Table of contents
Reading Time: 3 minutes

Containerization has been the leading mode for application deployment today. So tools like Docker has been in the rise. But the Docker containers are built for the purpose of deploying an application and hence a single process. So, Docker containers are not meant to run an Operating System. But what if we want to run multiple Operating Systems on a single host OS. People will opt for virtual machines as a solution to host multiple Operating System. But let’s be honest, virtual machines need hypervisors for resource provisioning which makes the virtual machines way slower than Docker containers when spun up. What if there is an alternative to Virtual Machines which works just as good as Virtual Machines and are as fast as Docker containers.

Linux Containers

LXC and LXD are key terms that we will focus on when we talk about Linux Containers. LXC stands for Linux Containers. It helps in virtualizing operating systems without the need for additional kernels. In the case of Hypervisor, every virtual machine comes packed with its own kernel. Unlike Hypervisor, LXC uses the host kernel to host all the other OS.

LXD on the other hand is an extension to the LXC. LXD provides a REST API that connects to the LXC software library, libxlc. It creates a daemon which we can access locally using Unix Socket or via network using HTTP.


The above diagram provides a comparison of the architecture of Virtual Machins, LXC and Container. So we can see in the above diagram, there is no underlying Hypervisor to spin up the instances. This makes them faster compared to Virtual Machines.


The installation guide is provided in the References part. You can refer to it to install LXC and LXD.

# After the lxd is installed we need to add the current user to lxd group to avoid writing sudo before commands
sudo usermod -aG lxd $USER

# Logout and then Login again

# To initialize the lxd daemon, execute the command and select the required options
lxd init

# To check lxc version
lxc version

# To get help and list and description of the various commands
lxc help

# To get help regarding particular command
lxc help <sub-command>

# To get the list of remote repositories form where the machine images will be pulled,
lxc remote ls

# To get list of images present in a repository
lxc image ls <repository-name>:<name>

# We dont always need to enter the full name to get the image. A part of the image name will also work.
lxc image ls images:ubu

# The above command will provide the list of images of name <name> or starting with <name> present in that repository

# To launch a container
lxc launch <repo-name>:<image-name> <container-name>

# To launch a Ubuntu Container,
lxc launch images:ubuntu/21.10 ubuntu

# To list running containers
lxc ls

# To stop the above running container
lxc stop ubuntu

# To delete a stopped container
lxc delete ubuntu

# To create a container from another container
lxc copy ubuntu ubuntu1

# The container ubuntu1 above will be created as a stopped container. To start a stopped container
lxc start ubuntu1

# To execute a command inside a container
lxc exec container-name command

# To get an interactive shell in a ubuntu container  execute
lxc exec ubuntu1 bash

# To login to container with a different existing user and group,
lxc exec ubuntu1 --group 1000 --user 1000 bash

# In ubuntu container, an user ubuntu with UID and GID 1000 exists. 

# To get info on a container,
lxc info container-name

# To set the memory and CPU limit in a container execute
lxc config set ubuntu1 limits.memory=1024MB limits.cpu=1

# To verify that the limits are set, exec into a container and execute

free -m     # To check the allocated memory
nproc       # To check the allocated CPU

In this blog, I have provided a guide to kick-start your use of Linux Containers. It is a startup guide that will help you to launch and use Linux Containers. I have provided the installation guide for lxc and lxd in the references section.

This image has an empty alt attribute; its file name is blog-footer.jpg

Written by 

Dipayan Pramanik is a DevOps Software Consultant at Knoldus Inc. He is passionate about coding, DevOps tools, automating tasks and is always ready to take up challenges. His hobbies include music and gaming.