In this post I will shortly introduce LXC containers, why they can be useful and their basic usage on Debian host.

Why to use LXC containers? The core concept of LXC containers is to provide lightweight alternative to virtual machines. While Docker containers purpose is to containerize an application in isolated environment, LXC containers role is to containerize linux operating system.

Installation

Prerequisites and installation steps can be checked on the official LXC page: Click here.

I'm using Debian VM, so in my case it will be:

sudo apt install lxc
sudo apt install lxd

lxd (Linux Container Daemon) package provides more user-friendly interface and has some additional features for managing LXC conateiners, so we will install it also.

Setup and basic usage

In order to init lxd daemon run following command:

sudo lxd init

For this example I accepted all default settings:

Now we can list all created containers (which will be empty list at that point).

sudo lxc list

Let's create and start ubuntu 22.04 image container with name container1 (if you want to create it, but not start use init option instead of launch).

sudo lxc launch ubuntu:22.04 container1

After pulling image if everything goes well we should see container1 running.

To execute any command inside the container simply run:

sudo lxc exec container1 <command>

To get container's bash shell we can execute following command:

sudo lxc exec container1 bash

If we wanted to start, stop, delete, pause (send SIGSTOP signal to all container processes) or clone (copy) container we can run following commands:

sudo lxc start container1
sudo lxc stop container1
sudo lxc delete container1
sudo lxc pause container1
sudo lxc copy container1 container2

Another useful utility is the possibility to pull or push files from host from/into the container. In order to push any file (testfile in this case) use following command:

sudo lxc file push testfile container1/tmp/testfile

In order to pull testfile back use:

sudo lxc file pull container1/tmp/testfile testfile

We can also check info about the container by using:

sudo lxc info container1

It can be useful in debugging to gather info about configuration.

That's all for this post, I hope you learnt something useful. In next one I will show how to access LXC container from our local network, because for now we can access our container only from the next machine.