Totally beginner: is LXC/LXD container what I need?

Hi all,

I am a total beginner with LXC/LXD and I would like to understand its concept, and try to figure out if this tool could provide me with a functionality I need.

Functionality: I work with two different Linux distributions: Nix OS at home, Debian at job. For this reason I would like to have a kind of Debian environment in Nix OS. It was considered: VM (like virtual box) and containers. The second one is more appealing to me as it feels more like doing a ssh to my own machine :thinking:

Questions: My main question (beside if containers is the solution to the goal above) is related with the file system.

  1. How the containers LXC/LXD works regarding the file-system? I understood that they will create a large image file that somehow is mounted. But actually, I could see from Nix OS the file hierarchy of the ‘Debian container’ although apparently it is a brtfs… (I had to select this option after lxd init) and I have an ext4 file system. What I misunderstood here? Probably there is a brtfs image mounted on the path where I can see the files. But then, where is this image physically?

  2. Essentially I understand that we have three steps:
    a. [daemon init] > lxd init
    b. [create container] > lxc launch images:debian/buster/amd64 nixdebian
    c. [run the container] > lxc exec nixdebian -- /bin/bash

    Could I select where the container is stored? Do I need to create a brtfs partition for it?
    Within this three main step where I can select the location of the containers?

  3. The fact that Debian container might be a brtfs file on my ext4 could harm anything? :grimacing:

Thanks for your help!

When you use LXD, you create one or more storage pools (used for storage of containers, container images). There is a good choice of storage backends for a storage pool, such as dir (uses the filesystem of the host), ZFS, btrfs, etc. For ZFS, btrfs, etc you have the option to create them inside a file (as a convenience) or use a dedicated partition. It is best to use a dedicated partition if you have it. If you have two extra disks, you can even get ZFS to use them in some RAID configuration for better performance!

LXD manages the full lifecycle of a container in the storage pool. If you have more than one storage pools, you can move containers from one to another. When you are about to launch a container, you can specify in which storage pool to put it (LXD 3.18 or higher). See lxc launch --help for more.

When you lxc launch a container, you essentially perform lxc init and then lxc start. All three are valid commands but most users would use lxc launch to get the container initialized and running in one go.
With launch, the container is running. Note that lxc init is very different from lxd init.

When you lxc exec you just run a program inside an already running (live) container. You cannot exec if the container is not already running.

If you can afford a dedicated partition for your storage pool, use it instead of placing the storage pool in a file.

1 Like

For your additional questions:

The containers are separate from your host’s file system. You can create a container, get into the container, perform a rm -fr / in the container, and the host is not affected at all. Whatever happens in a container, stays in the container.

Depending on your choice of the storage pool, you may see the container files under /var/snapd/lxd/.. (for the snap package) or /var/lib/lxd/ (for the deb package). The container files are mounted under a subdirectory over there. But the container cannot move up the subdirectory and access your host’s files.

I think you are using a deb package of LXD. Run lxd --version to find the version. The new cool LXD options such as moving containers around, can be found in somewhat newer versions of LXD.