Linux Containers (LXD)
This container system manager is an alternative to Docker that is easy to learn and use.
Table of Contents
- Introduction and installation
- Launch containers
- Execute commands
- Stop a container
- Delete a container
- List containers
- List downloaded images
- Delete an image
- List remote images
- Configure instances
Introduction and installation
LXD is similar to Docker but it has some differences. You can use LXD for containers or virtual machines. The syntax is similar but not the same.
You can install
lxd by installing its
snap package or by installing the version of your system official repositories.
Once installed, you need to configure LXD by running
lxd init (with root privileges). It will start an interactive configuration process. There is more info about this process on this link. You can avoid this process with the command
lxd init --minimal (it will create a non-optimized minimal setup with the default options).
$ sudo lxd init Would you like to use LXD clustering? (yes/no) [default=no]: Do you want to configure a new storage pool? (yes/no) [default=yes]: Name of the new storage pool [default=default]: Name of the storage backend to use (btrfs, dir, lvm, zfs) [default=zfs]: Create a new ZFS pool? (yes/no) [default=yes]: Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: Size in GB of the new loop device (1GB minimum) [default=30GB]: 15GB Would you like to connect to a MAAS server? (yes/no) [default=no]: Would you like to create a new local network bridge? (yes/no) [default=yes]: What should the new bridge be called? [default=lxdbr0]: What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: Would you like the LXD server to be available over the network? (yes/no) [default=no]: Would you like stale cached images to be updated automatically? (yes/no) [default=yes] Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
For the following sections, we are going to use
lxc, the CLI client tool for working with LXD containers.
Download an image from the repositories (check
lxc remote list) and create a container.
lxc launch <image>:<tag> <container name> # lxc launch ubuntu mycontainer # lxc launch ubuntu:21.04 mycontainer
lxc launch <remote>:<image path> <container name> # lxc launch images:ubuntu/22.04/cloud/amd64 mycontainer
You can execute commands against your container (you can even execute
/bin/bash in order to go to container’s shell). We use
-- to specify that there are no more command parameters.
lxc exec <container name> -- <command> # lxc exec mycontainer -- /bin/bash
Stop a container
You can stop a running container with this simple command (the same as Docker).
lxc stop <container name> # lxc stop mycontainer
Delete a container
Delete an stopped container with
lxc delete (you can delete a running container with the
lxc delete <container name> # lxc delete mycontainer
List downloaded images
lxc image list
Delete an image
lxc image rm <image fingerprint> # or lxc image delete <image fingerprint> # lxc image delete a84023248421
List remote images
# Note the space after the : lxc image list <remote>: <filter> # lxc image list images: ubuntu 20.04
lxc image list --helpfor more info about the output of this command.
lxc config and:
set <container name> <parameter name> <parameter value>
lxc config set debian limits.cpu 1
lxc config set debian limits.memory 1GiB
get <container name> <parameter name>
lxc config get debian limits.cpu
lxc config --help for more info.
In some systems, your user must be part of
lxdgroup to be able to use LXD with an unprivileged user.
# Remember to be a root user or use "sudo" to run this command usermod -aG lxd <username>
- You may need to log out and log in for the change to take effect.
If, when you run
lxd init, it displays an error, try starting (and enabling)
sudoif you are not a
systemctl enable --now lxd.service
When you launch a container, it shows an error. If you look for the log, it says something like this:
newuidmap failed to write mapping "newuidmap: uid range [0-1000000000) -> [1000000-1001000000) not allowed":.
/etc/lxc/default.confto add these lines:
lxc.idmap = u 0 100000 65536 lxc.idmap = g 0 100000 65536
Create or edit two files:
/etc/subgidand add/modify these lines:
If you have any suggestion, feel free to contact me via social media or email.