In the next article we are going to take a look at LXD containers. A colleague already spoke about them on this blog some time ago. In that article I already made it clear that these containers are useful for different reasons. They isolate applications from the rest of the system, they are portable, easy to clone and / or move to other operating systems. In the case of Gnu / Linux, they work the same way in any distribution, without the need for adaptations.
Docker is a popular solution designed to contain a single application such as a MySQL database server. LXD is similar in some respects, but it is designed to contain a complete operating system. LXD is not going to virtualize hardware like QEMU o VirtualBox. This means that it is very fast and offers almost native execution speed.
As an example of use, we could create an LXD container, install a database server and an http server. There we will have the possibility of creating a website with WordPress and if necessary we can change from cloud to cloud simply by moving this LXD container where necessary. Since it's easy to clone a container, your website could even be uploaded to multiple cloud providers to create a redundant and highly available setup.
Install and configure LXD
To install LXD you just have to open a terminal (Ctrl + Alt + T) and install. What's more we can also install the ZFS utilities. These will help us speed up some operations and save disk space when working with containers.
sudo apt install zfsutils-linux lxd
For start LXD configuration, in the same terminal we write:
sudo lxd init
Here we can press Intro to select the default values.
Find and start an LXD distribution image
For see a list of Ubuntu images, in the terminal we write:
lxc image list ubuntu: arch=amd64|head
It can omit arch = amd64 if you need images for other architectures. In the image above, the results have been limited (with | head) to make it easier to read.
As you could see in the screenshot above, the fingerprint of Ubuntu 18.04 (dcbc8e3e5c2e) I left it highlighted. If you are interested starting a container with that distribution should use that footprint. The command to execute would be:
lxc launch ubuntu:dcbc8e3e5c2e
Manage LXD containers
For list all created containers, you just have to write:
lxc list
The spine 'IPV4'is especially important if you have any services running on that instance. For example, if you were running an Apache http server, when writing the IP "10.191.112.88”The web site hosted in the container would be displayed in the browser.
For stop a container, you just have to write:
lxc stop nombre-contenedor
This can be time consuming, or fail with non-Ubuntu distributions. If this happens, you can always use systemctl power off to stop it.
If all else fails, you can force stop with:
lxc stop nombre-contenedor --force
For start with a container you should write:
lxc start nombre-contenedor
If you would like to move inside the container, run:
lxc shell nombre-contenedor
Inside the container you can install programs with 'sudo apt install'and do whatever else you would do on a normal Gnu / Linux distribution, for example, setting up an Apache server.
Anytime get out of the container, just type:
exit
Transfer files to / from LXD Containers
For upload a file to your container, use the following syntax:
lxc file push /ruta/al/archivo/local/nombre-archivo nombre-contenedor/ruta/al/archivo/subido/nombre-archivo
Include the name of the file that will be created, not just the directory where you want to leave it.
For load a directory instead of a file:
lxc file push /ruta/al/directorio nombre-contenedor/ruta/al/directorio/remoto --recursive --verbose
For download a directory from the container to your operating system main:
lxc file pull nombre-contenedor/ruta/al/directorio/remoto ruta/al/directorio --recursive --verbose
This covers the basic use of LXD containers. There are more advanced features such as snapshots, limits on resources such as CPU and RAM, clone containers, etc. All this and more can be consulted in the official documentation, In the project website or on your page GitHub.