Linux Containers (LXC) are managed by LXD as if they were Virtual Machines, allowing them to take snapshots, have distinct IPs, and the content data is not lost when the container is shutdown.

Containers can be managed using profiles to define network and other configuration parameters such as limiting the maximum amount of CPU or RAM used by a single instance.

Internally, the containers can communicate using containerName.lxd because there is a domain name service set up by default.

LXD vs Docker

While LXD is a system container type that contains a whole operating system functionality, Docker is an application container type that usually only runs single applications and abstracts storage, network, logs… from the user.


INSTALLATION

The second command line (optional) enables the Web-UI.

sudo snap install lxd
sudo snap set lxd ui.enable=true
sudo systemctl reload snap.lxd.daemon.service
sudo usermod -aG lxd yourUserName
sudo lxd init

Note: it is not necessary but is recommended to add your user to the lxd group to allow manipulating the containers without using sudo every time. It might require logging out and back in again to apply to the new group.

WEB-UI SETUP

The Web-UI is only available from the version LXD 5.14 or newer. Navigate to https://127.0.0.1:8443/ from the the local machine or add the network address if from a remote:

Completed!


MOST POPULAR COMMANDS

  • lxc help
    • Shows the list of commands.
  • lxc remote list
    • Shows the list of remote repositories.
  • lxc image list ubuntu:
    • Lists the content of the officially supported images.
  • lxc image list images: ubuntu jellyfish
    • Searches for an image that contain the names ubuntu and jellyfish.
  • lxc image alias list images:
    • Lists the images in a more user-friendly format.
  • lxc launch ubuntu:22.04 u22lts
    • Launches (downloads if no local image exists and starts) with the given name u22lts.
  • lxc launch images:ubuntu/20.04 u20lts -c limits.cpu=1 -c limits.memory=256MiB
    • Launches a container and sets configuration to the maximum allowed CPU and RAM usage.
  • lxc list
    • Lists the existent containers.
  • lxc list –columns “ns4S”
    • Lists the containers in a table with columns: name, status, IPv4, # of snapshots.
      • 4 – IPv4 address
      • 6 – IPv6 address
      • a – Architecture
      • c – Creation date
      • n – Name
      • p – PID of the container’s init process
      • P – Profiles
      • s – State
      • S – Number of snapshots
      • t – Type (persistent or ephemeral)
  • lxc list security.privileged=true
    • List those container filtering by a propriety.
  • lxc exec u22lts apt update && apt ugrade -y && apt install nginx -y
    • Executes a command inside a container.
  • lxc exec u22lts –env KEY=VALUE script.sh
    • Executes an application insite the container providing an environment variable.
  • lxc exec u22lts bash
    • Gets a Bash shell in the container.
  • lxc file edit u20lts/etc/passwd
    • Edits a file insite the container.
  • lxc stop u22lts
    • Stops the container.
  • lxc start u22lts
    • Start the container.
  • lxc restart –force u22lts
    • Restarts the container with forcing.
  • lxc snapshot u22lts snapshot01
    • Creates a snapshot of the container and gives it the name snapshot01.
  • lxc restore u22lts snapshot01
    • Restores a snapshot.
  • lxc delete u22lts/snapshot01
    • Deletes a snapshot.
  • lxc config set u22lts boot.sutostart 1
    • Defines the configuration to auto start the container during the boot of the host.
  • lxc config set u22lts boot.sutostart.delay 60
    • Defines the amount of seconds after boot to start the container.
  • lxc config set u22lts boot.sutostart.order 3
    • Defines the order for auto starting the containers.
  • lxc config set u22lts limit.memory 1GB
    • Defines the maximum amount of RAM the container is allowed to use on the fly (without stopping the container).
  • lxc config edit u22lts
    • Open an editor with the configuration file for the container.
  • lxc info u22lts
    • Gathers information about the container.
  • lxc config show –expanded u22lts
    • Shows the configuration applied to the container with expanded details.
  • lxc delete u22lts
    • Deletes the container.
  • lxc copy u22lts u22lts-copy
    • Copies (clones) the container and defines a name to the new one.
  • lxc copy u22lts/snapshot01 u22lts-from-snapshot01
    • Copies (clones) the container and defines a name to the new one.
  • lxc move u22lts u22lts-renamed
    • Renames a local container or moves the container to another host.
  • lxc move u22lts/snapshot01 u22lts/snapshot01-renamed
    • Renames the snapshot of a local container.
  • lxc file push fileName.zip u22lts/root/
    • Pushes a file fromt he host to the root of the file system the container.
  • lxc file pull u22lts/root/fileName.zip .
    • Pulls a file from the root of the containers file system to the host.
  • lxc file pull u20lts/root/etc/hosts – | less
    • Pulls a file from the container and pipes into a consecutive command.
  • lxc profile list
    • Lists the existing profiles.
  • lxc profile default
    • Shows the configuration of the profile.
  • lxc delete –force u22lts
    • Forces the deletion of the container without stopping it.
  • lxc network list
    • Lists the network adapters of the host.
  • lxc network show lxdbr0
    • Shows details of the network interface.
  • lxc network create myNetworkName –type=physical parent=br0 –target=u22lts
    • Sets up a physical network to a container.
    • Other types of network are: bridge, ovn, macvlan, and sriov.
  • lxc network set myNetworkName dns.nameservers=8.8.8.8
    • Configures the network to use a specific DNS server.
  • lxc network forward port add myNetworkName 192.168.1.2 tcp 80,8080-8088 10.1.1.2 80,8080-8088
    • Does port forwarding from an external IP and port to an internal IP and port.
    • Suports TCP and UDP, with single port, lists or ports, or range of ports.
  • lxc storage list
    • Lists the storages.
  • lxc storage create myStorage btrfs
    • Creates a storage. The btrfs is recommended for running docker inside LXD. It is supports with best performance the layering that Docker requires.
    • Other supported types: dir, ceph, cephfs, lvm, and zfs.
  • lxc launch images:ubuntu/22.04 myContainer
    • Creates a container from Ubuntu 22.04 image to run Docker.
  • lxc storage volume create myStorage myVolume
    • Creates a volume inside the storage.
  • lxc storage volume list myStorage
    • List the volumes inside the storage pool.
  • lxc config set myContainer security.nesting=true security.syscalls.intercept.setxattr=true security.syscalls.intercept.mknod=true
    • Enabling the necessary configuration for running Docker. It requires the container do be restarted.