Linux Containers (LXC) – Keep your development machine clean and lean.

Linux Containers (LXC) – Keep your development machine clean and lean.

If you use Linux on your development machine and you always try out new technologies. It probably involves installing numerous packages and dependencies. Over the time these packages pile up, making your system bulky and some dependencies might even cause conflicts with other installed packages. Making their maintenance and carrying out updates harder. I faced similar problems too, and I have tried to find solutions for it. Let me show an easy way to keep your machine clean, lean and mean! using Linux Containers (LXC).

What are Linux Containers (LXC)?

One of the ways to solve this problem is to use a virtualisation platform; ergo, Linux Containers or LXC. LXC is an operating-system-level virtualisation technique. You can run multiple, isolated Linux systems (containers) on a host machine using a single Linux kernel.

LXC makes use of Kernel features such as cgroups and namespace isolation which offers an environment as close as possible to the one you’d get from a VM (ex. VirtualBox) but without the overhead that comes with running a separate kernel and simulating all the hardware.

Installation:

Install `lxc` and some necessary dependencies such as lxc-templates and `wget` which also pulls some predefined templates for many of the well known Linux distributions. These templates can be found in `/usr/share/lxc/templates/`.

$ sudo apt install lxc lxc-templates wget bridge-utils

If you are behind a proxy:

If your computer is behind a proxy server, there are a couple of things to take care of before we can move further. Firstly we must set up a proxy for `wget`. wget is a free software package for retrieving files over the Web. Its configuration file is at `/etc/wgetrc`.

$ sudo vim /etc/wgetrc

Note: I’m using `vim` here, but you can substitute that with any editor that you are familiar with like `nano` or `gedit`.

Add your proxies to `wgetrc`.

https_proxy = https://yourproxy:port/
http_proxy = http://yourproxy:port/
ftp_proxy = ftp://yourproxy:port/

Create a container:

Here I am showing an example of an Ubuntu container. For the first time, this might take a few minutes depending upon your internet connection. Any future creations are going to much faster as the templates are cached. Note the username and password displayed in the terminal output (Usually, both the username and password are `ubuntu`).

$ sudo lxc-create -t ubuntu -n test_ubuntu

After installation you can list all available containers using:

$ sudo lxc-ls

Start the container:

Running our container is stupid simple!:

$ sudo lxc-start test_ubuntu

You can check if the container is running or not by doing:

$ sudo lxc-ls --fancy

Note that the above command also displays the containers IP address.

Access the running container:

You can log into your running container using the `lxc-console` command. This command attaches your current terminal to the console of the running container.

$ sudo lxc-console test_ubuntu

The network connection inside the container should work without any modifications. However, if you’re behind a proxy don’t forget to set the same proxy for apt. You can do this by creating a new file:

$ sudo vim /etc/apt/apt.conf

Add the proxy in the following format:

Acquire::http::Proxy "https://yourproxy:80/";
Acquire::https::Proxy "http://yourproxy:80/";
Acquire::ftp::Proxy "ftp://yourproxy:80/";
Acquire::socks::Proxy "socks://yourproxy:80/";

You can check if the proxy is working by running:

$ sudo apt update

That’s it! You can now set up your stacks like LAMP or MEAN or whichever you might prefer. However, we can make our container a bit more useful by adding GUI functionality to our container.

Setting up the GUI:

Not having a GUI significantly reduces the number of apps you can use in a container, so let’s fix it. X Window system is a framework which provides a necessary GUI environment and is used by most Linux distributions (including Ubuntu). The idea is to access the host machine’s X server from the container using SSH.

Firstly update your ubuntu container:

$ sudo apt update
$ sudo apt upgrade

Install xauth and dbus interface for X server:

$ sudo apt install xauth dbus dbus-x11

Now you can install any GUI based applications; We’ll install firefox as an example.

$ sudo apt install firefox

firefox also needs canberra-gtk-module to work.

$ sudo apt install libcanberra-gtk-module libcanberra-gtk3-module

SSH into the container:

Open a new terminal window and SSH into your container while enabling X11 forwarding.

$ ssh -X [email protected]

Note: You can find your container’s IP address using the command `$ sudo lxc-ls –fancy`.

Launch firefox:

We can now start using our application.

$ /usr/bin/firefox

Note: You can find the location of any package using the which command (Ex. `$ which firefox`)

LXC clones:

When we use a container to try out new things and experiment, it is likely that we mess one up in the process. Hence, it is a good idea to backup or to keep several copies of our container. So we keep one up-to-date container as a base (we will not do anything in this container except for updating it occasionally) and then, as needed we create copies (snapshots) of it.

This strategy is very efficient because creating snapshots minimises the disk space used. As a snapshot uses an overlayfs mount to only write out the differing changes to the disk (Copy-on-write)

Clone a container:

$ sudo lxc-copy -n base_container -N new_container -B overlayfs -s

The snapshots can be started/stopped like any other containers. You can also destroy a snapshot or a container if you no longer need them.

Stop a running container:

$ sudo lxc-stop test_ubuntu

Delete/Destroy a container:

This command completely removes the container and all its data from your system.

$ sudo lxc-destroy test_ubuntu

 

That’s it now you can continue your learning process without having to worry about messing up your system. I blog about many domains in programming and have done some cool projects feel free to check them out if you’re interested.

Happy coding!

P.S:

You can also consider making a small donation to support me. Your donation will directly contribute to the running cost of this website and hopefully my college too 🙂

Paypal: https://www.paypal.me/sapneshnaik  |  UPI: [email protected]

 

Leave a Reply

avatar
  Subscribe  
Notify of