Post

Using LXC containers

Using LXC containers

My LXC start with Proxmox

Recently I started in my homelab/homeserver with the use of Proxmox. That works out quit well. It is an old NUC with 32 Gb memory and a 4 core I5. It may be a bit old, but it works quit well. Initially, I started making Virtual machines (VMs), since I am already quite familiar with how to use them. However since I realise how, from a performance point of view, containers make more sense I finally took the time to learn how to use LXC containers in Proxmox.

This worked out quite well. LXD containers have the advantages of containers, such as that they use the kernel of the host system. However they behave very much the same as a VM. And the setup is even easier. You simply select the template and Proxmox, or rather LXD will download the image and do the rest. You can then login and use it quite similar as a VM.

the difference with Docker containers

They are quite convenient in this setup as there are a few noticeable differences:

  • The LXC containers are stateful. Although Docker containers are not for a good reason, it works out well for this usage
  • Proxmox sets them up properly with an IP in your Lan. In Docker, and Kubernetes for good measure, handling the network part is not super easy.

But similar to Docker, they are containers sharing the Kernel with the host system. With VMs you add the overhead of an extra kernel and preferably don´t overbook memory too much. You basically you slice up the host regardless on how heavy the VM is actually using the services. Containers conversely act just like another process in the host system. This is much more efficient.

in retrospect

Since I started with VMs and haven’t gotten around to setup again, there is still one VM running. The rest are already containers. If I where to do it again, I would make one LXC container acting as an NFS server with a large part of the NVME drive. And the services on LXC containers that get to mount a directory for file storage. That way you also handle the storage more efficient. I.e. on a 4Tb drive you could easily assign 3,5tb to the NFS server. And then have an LXC container with Jellyfin using it to store the mediafiles and an LXC container running Nextcloud using it also for file storage. Those containers can remain very small and you don’t have to know in advance how much each is going to need for filestorage as they share the space.

Don’t need Proxmox

Proxmox is great. However I do a lot mucking around on my desktop with a Ryzen 9. I used Virtualbox to create VMs and/or Docker. I want my ‘host’OS to remain my normal desktop OS (Linux Mint). Obviously though you can run LXC containers quite well on it. So I tried this and I like the results.

LXD/LXC

Running LXC containers without a specialised distro such as Proxmox is not that hard. You have to be comfortable with teh cli (command line interface). But it works actually quite well. To understand how, first an explanation of how it is setup. To run LXC containers you install LXD. LXD is the container management part. It provides the backend. Most commands however are done with the command LXC. I assume LXD means the daemon and LXC the client. There is an excellent video of Learn Linux TV on Youtube which explains this much better than what I can do here. What is really nice is that LXD will get the files for images of the containers from repositories. By default it uses the repository of Linuxcontainers.org the home of LXC. And you will find many of your favourite distributions in the images

LXD also does virtuals?

After following the mention Youtube video getting started was easy. However when I listed the available images in the repository I noticed there were also images of virtual machines? Turns out you can use LXD also to start and manage VMs. It will use Qemu under the hood. Why would you do that? Well for instance if you want to use it to setup a full desktop. So I gave that a go. It took a little tinkering. I needed to setup the networking so that I could reach them via TCP/IP. But that was not to hard. And then it was simply installing XRDP on the guest and connect with Remmina. To be fair, Virtualbox is a lot easier to use. Especially if you want some VMs to use NAT and others to use a bridge. But that is largely also a matter of getting better and familiar with LXD. So I will absolutely explore this further. For now it will not replace Virtualbox for me as the main driver. And unfortunately you can’t run them simultaneously on one host. But in the future that may certainly happen.

Some commands / cheat list

When using LXC I created a table with handy commands:

CommandoExplanation
lxc remote listshow list of repos LXD can draw images from
lxc image list images: debianshow list of images, but limit the search to images with debian in the name. Filling out nothing will show all images
lxc launch images:ubuntu/focal mycontainer –type=containerCreates the container. The part ‘mycontainer’ is the name you choose for the container –type ensures it starts a container only
lxc listshows running container
lxc exec mycontainer – apt updatecommand apt update in the container. Note the space after the two dashes
lxc start mycontainerstarts container
lxc stop mycontainerstops container
lxc restart mycontainerrestarts container
lxc delete mycontainerDeletes container, first stop the container
lxc snapshot mycontainer snapshotnametakes a snapshot of container mycontainer with the name snapshotname
lxc info mycontainerdetailed information of the container
lxc delete mycontainer snapshotnameDeletes the snapshot
lxc restore mycontainer snapshotnameRestores the snapshot
lxc config set mycontainer boot.autostart 1start bij start pc
lxc config set mycontainer limits.memory 4GBLimits amount of memory
lxc config set mycontainer boot.autostart.order 8Sets bootorder. Number can be freely chosen, but it will obviously start with the lowest number
This post is licensed under CC BY 4.0 by the author.