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/.

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.

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.

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).

After installation you can list all available containers using:

Start the container:

Running our container is stupid simple!:

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

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.

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:

Add the proxy in the following format:

You can check if the proxy is working by running:

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:

Install xauth and dbus interface for X server:

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

firefox also needs canberra-gtk-module to work.

SSH into the container:

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

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.

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:

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:

Delete/Destroy a container:

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

 

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!

 

 

Leave a Reply

avatar
  Subscribe  
Notify of