When I first started to learn how to code, I started writing hello worlds in Python. As many others did too. However, eventually you will want to build something greater and you come accross an increase in complexity, system requirements, software dependencies… The list goes on. Or you have that Java project that uses Java 7 but you have others that use Java 8 and you want to keep your machine up-to-date… Or you want to check if your project would run in an environment you don’t have and don’t want to install another partition or create a VirtualBox machine for it because, after all, your program is terminal-based.
The goal of this post is, thus, to explain how you can setup a (safe) development/testing environment using LXD/LXC containers. As you may have guessed, I’m marvelled by this technology.
LXC is a well known set of tools for virtualization. LXD is a next generation system container manager.
You start by installing it in your system. It works in Linux, MacOs and Windows (although this last one is moving sands to me). In Ubuntu, the command is
sudo apt install lxd lxd-client. You can install this via snap too (I personally love snaps, so I always go for snaps). The next step is to run its initial configuration which you can safely set everything to its defaults. To start the wizard, run
sudo lxd init. You should only have to run this one time only.
To create your first ever container, you can run
lxc launch ubuntu:16.04 myFirstContainer. Here’s where things get interesting: You can create any container for pretty much almost all linux distributions you can find. You can do so by running
lxc launch images:the_image_name/version/cpu_arquitechture containerName. I would like to point out images refer to the remote image server. You can have access to the full list here: https://us.images.linuxcontainers.org
If you have a project requiring you to run a specific version or if you want to manually test things in a given version, these containers allow you to do in the comfort of your own terminal. I personally started using it because I contribute to a project that only builds right in the Ubuntu LTS version. Thus I cannot develop in the less stable versions.
There are two ways to run containers. You either login to your machine, or run
lxc exec myFirstContainer -- some command which will execute the command in the container without logging in. Depending on what the task is, I usually prefer logging in to the machine and get the work done. One limitation I found was to get my SSH keys into the container. I forcibly had to do
lxc file push myFirstContainer ~/.ssh/.. That way I could eassily get the source code of my projects from GitHub using SSH.
From thereon, you can go even a step further and allow others to use your image to automatically test. Lets just say you can move containers running
lxc move myFirstContainer landingAddress. You can read more about this part here: https://stgraber.org/2016/04/12/lxd-2-0-remote-hosts-and-container-migration-612/ In fact, I’ve caught your attention using these containers, the best guide out there regarding these is this series.
Now you are able to run LXC/LXD containers on your machine, allowing you to setup specific development environments that suit your project’s needs, without risking to harm your machine’s integrity. There are more benefits to LXC/LXD that I didn’t cover, namely the resource management, which can either be dinamic or static, and the API LXD provides.
Thank you for your time reading, I’ll see you next time. gsilvapt