LXD Cheatsheet

Although I haven't encountered them in the wild so far, I wanted to know a little bit about this technology and compiled a small list of things you need to start.


this should be an image

Kind of the official logo

Basic terminology

Linux-Containers are basically containers (who would have guessed 🙃). But unlike Docker, the state and data inside the container is persistent and there are no layers. That means you don't have to worry about Volumes or directory mounts, the containers work more like virtual Machines in that regard. They are also only available on Linux, because this technology uses specific functions inside the Linux kernel (cgroups and namespaces).

There are two words you need to know:

  • Image: Is excactly what you think it is... An operating system ready to deploy
  • Container: An instance of an image.
  • lxc: The command to control a container, the network interfaces etc.
  • lxd: The command to control multiple containers with. In a more advanced way (for clustering and such). It is like a higher version of lxc.

Sadly, there are no guis available to manage this stuff, except for Proxmox, but that is a full blown hypervisor for way bigger setups, than this.


Getting started


In my case, I used snap install lxd to install lxd. You should also have a seperate user set up to work with this (not root). After that you simply type lxd init and answer some questions. You should use the default settings (besides storage type = zfs, only use this if you have more than one drive available to setup zfs properly, otherwise take dir, that stores everything to your local drive). One more note: Set make lxd server available to the network = no unless you really have to and know what you are doing.


Pulling Images


  • Show all remote repositories (that's where you pull images from): lxc remote list
  • List all images inside a repository: lxc image list <repository_name>:
  • Search for images inside a repository: lxc image list images: <keyword_1> <keyword_2> ...

Interacting with the containers


General
  • Show all running containers: lxc list
  • Start a new container (image will be pulled if not locally available): lxc launch <repository_name>:<image_name> <container_name>
  • Running a command inside a container: lxc exec <container_name> -- <command(s)>
  • Showing information about a container: lxc info <container_name>

Start/Stop/Restart/Delete
  • Start a container: lxc stop <container_name>
  • Stop a container: lxc start <container_name>
  • Restart a container: lxc restart <container_name>
  • Delete a container (has to be stopped beforehand): lxc delete <container_name>

Snapshots
  • Create: lxc snapshot <container_name> <snapshot_name>
  • Delete: lxc delete <container_name>/<snapshot_name>
  • Restore: lxc restore <container_name> <snapshot_name>

(Hint: You can view all of the created snapshots with lxc info)


Changing the configuration of a container
  • View config: lxc config show <container_name>
  • Memory ceiling: lxc config set <container_name> limits.memory 2GB
  • Make the container start automatically: lxc config set <container_name> boot.autostart 1
  • Setting a boot delay:
    1. lxc config set <container_name> boot.autostart.delay <seconds> makes the container start with a delay.
    2. lxc config set <container_name> boot.autostart.order <number> gives the container a "magnitude" (lower numbers start before higher numbers).

Networking
  • Show all networks: lxc network list
  • Show information about a network: lxc network show <network_name>
  • Edit a network: lxc network edit <network_name>

The list is definitely not complete, but I might add more stuff to it later.