Getting started with LXC

Linux Containers (LXC) allows you to run applications in containers within your host operating system. Containers are isolated from your host operating system with their own OS, network and behave as independent machines for all practical purposes.

You can run any number of containers to isolate your applications limited by your hardware capacity.

Containers are similar in functionality to virtualization but with some key differences.

Virtualization involves emulating the hardware layer and the operating system while LXC only emulates the OS layer which makes it extremely lightweight and fast.

The emulation is enabled by cgroups and namespaces in the Linux kernel so LXC cannot emulate an OS other than Linux or a specific Linux kernel.

The immediate benefit of LXC is speed. Because it is not emulating the hardware layer it operates at near bare metal speeds. So you get all the benefits of virtualization but without the performance overhead. This delivers tremendous benefits in portability and efficiency to users.


LXC is great way to deploy applications. Let's take a web stack like PHP, MySQL, Nginx and an app, you could install it in a VM but with a performance overhead.

With a container you get near bare metal speeds and lower resource usage that allows you to scale efficiently.

You can maintain a clean and minimal host system that can be recreated quickly, and use lightweight containers to run your apps which because they are deployed in a container are now portable and can be moved across systems easily and cloned, backed up and deployed in seconds!

Using LXC

To get started, install LXC from the Flockport repo with our 5 minute guide designed to be easy, comprehensive and prevent user frustration and damaged computer peripherals.

LXC once installed is very simple to use. The first step is creating a container.

Here is quick walkthrough of LXC basics

LXC provides OS templates for the most popular Linux distributions; Debian, Ubuntu, Centos, Fedora etc.

These templates are customised to operate in a container environment.

To create a container

lxc-create -n mycontainer -t debian

n - container name - any name you want
t - template name for the container OS ie debian, ubuntu etc

You may also specify the architecture. The default, when left unspecified is amd64

lxc-create -n mycontainer -t debian -- -a amd64

To see a list of container OS templates available for use in your LXC installation (depending on where you distribution installs LXC, it could also be in '/usr/share/lxc' so check)

ls /usr/local/share/lxc/templates/

lxc-alpine     lxc-centos    lxc-fedora        lxc-oracle  lxc-ubuntu-cloud
lxc-altlinux   lxc-cirros    lxc-gentoo        lxc-plamo
lxc-archlinux  lxc-debian    lxc-openmandriva  lxc-sshd
lxc-busybox    lxc-download  lxc-opensuse      lxc-ubuntu

You can also use a special type of container OS template called 'download'. This downloads updated container OS templates from the LXC server.

root@debian:~# lxc-create -t download -n test
Setting up the GPG keyring
Downloading the image index

centos 6 amd64 default 20140816_02:16
centos 6 i386 default 20140816_02:16
centos 7 amd64 default 20140814_02:16
debian jessie amd64 default 20140815_22:42
debian jessie armel default 20140815_22:42
debian jessie armhf default 20140815_22:42
debian jessie i386 default 20140815_22:42
debian sid amd64 default 20140815_22:42
debian sid armel default 20140815_22:42
debian sid armhf default 20140815_22:42
debian sid i386 default 20140815_22:42
debian wheezy amd64 default 20140815_22:42
debian wheezy armel default 20140815_22:42
debian wheezy armhf default 20140815_22:42
debian wheezy i386 default 20140815_22:42
fedora 19 amd64 default 20140814_01:27
fedora 19 armhf default 20140814_01:27
fedora 19 i386 default 20140813_01:27
fedora 20 amd64 default 20140814_01:27
fedora 20 armhf default 20140814_01:27
fedora 20 i386 default 20140814_01:27
gentoo current amd64 default 20140814_14:12
gentoo current armhf default 20140814_14:12
gentoo current i386 default 20140814_14:12
oracle 6.5 amd64 default 20140816_00:13
oracle 6.5 i386 default 20140816_00:13
plamo 5.x amd64 default 20140730_15:47
plamo 5.x i386 default 20140730_15:47
ubuntu lucid amd64 default 20140816_03:49
ubuntu lucid i386 default 20140816_03:49
ubuntu precise amd64 default 20140816_03:49
ubuntu precise armel default 20140816_03:49
ubuntu precise armhf default 20140816_03:49
ubuntu precise i386 default 20140816_03:49
ubuntu trusty amd64 default 20140816_03:49
ubuntu trusty arm64 default 20140816_03:49
ubuntu trusty armhf default 20140816_03:49
ubuntu trusty i386 default 20140816_03:49
ubuntu trusty ppc64el default 20140816_03:49
ubuntu utopic amd64 default 20140816_03:49
ubuntu utopic arm64 default 20140816_03:49
ubuntu utopic armhf default 20140816_03:49
ubuntu utopic i386 default 20140816_03:49
ubuntu utopic ppc64el default 20140816_03:49

We cover the use of 'download' template in the LXC advanced guide. For now let's use locally available OS templates to create containers. Once you enter the create command LXC will download the container OS as per the template selected, setup the container and give you the default login credentials.

This typically takes 2-5 minutes depending on your internet connection speed. Once downloaded, container OS's are cached locally for reuse so the next time you create a container with the same OS it will not be downloaded again.

LXC containers once downloaded are stored as individual folders in '/var/lib/lxc'. The individual container folder typically has 3 files; config is the container configuration file, rootfs is the container OS directory and fstab, which is used to mount host directories in the containers.

So now that you have a shiny new container it's time to start it.

lxc-start -n mycontainer -d

The -d option starts the container as a daemon and returns you to the shell. Without the -d option you can see the container boot in the terminal and eventually give you a login screen.

You can't exit this mode without powering off the container at which point you will be dropped back to your terminal so the -d option is the preferred way to start containers.

If you installed the package from the Flockport repo the container will be setup with networking and you can get its IP by running:

lxc-ls -f

lxc-ls gives your information about containers.

Now you can either access your container over ssh or by using LXC tools.

lxc-attach -n mycontainer

-n - containername

lxc-attach will drop you into the container root shell.

You can also use ssh or lxc-console tool to log in to your container.

ssh user@containerip

lxc-console -n containername

Now that you are in the container it's just like being in another OS. You can install apps etc. To exit the container and return to your shell just type exit.

If you are using lxc-console you need to use ctrl+a+q to return to your terminal. To poweroff the container type poweroff or halt.

Here is a list of the LXC commands available

lxc-config           lxc-console         lxc-info               lxc-stop 
lxc-attach           lxc-create          lxc-ls                 lxc-unfreeze
lxc-autostart        lxc-destroy         lxc-monitor            lxc-unshare
lxc-cgroup           lxc-device          lxc-snapshot           lxc-usernsexec
lxc-checkconfig      lxc-execute         lxc-start              lxc-wait
lxc-clone            lxc-freeze          lxc-start-ephemeral

Continue to Part II of the Getting started with LXC Guide

The second part of the guide will cover more advanced LXC functions.

Recommended Posts

Leave a Comment


Register | Lost your password?