LXC – Linux Containers
“Linux containers, Its an operating system based virtualization method, you can run your multiple isolated instances on a single host. It is similar to chroot(File system isolation) but it offers a lot. You can zip an LXC container and move it to any other host with the same processor architecture. LXC is the part of mainline Kernel it doesn’t require hardware emulation, Which makes it very light weight and very easy to use.”
So suppose if you have two application which you want to run on isolated environments, you can use the LXC. For setting up LXC you can follow the below mentioned steps.
In this blog, we will setup a Linux container with http website running on it. Following are the steps which you need to follow to setup the same.Operating System : Ubuntu14.04 LTS Packages: lxc
Command to install LXC:
[js] apt-get install lxc [/js]
The above command will install all the dependency to required for running a Linux container. After installing we will check the configuration by running lxc-checkconfig command.
In the above image, you can see everything is enabled. Now you can go ahead and start your first Linux Container.
[js]lxc-create -n httpContainer -t ubuntu [/js]-n: Name of the Container
-t: LXC Template (In our example we are creating a ubuntu base container. lxc-create command will read template files from “/usr/share/lxc/templates” directory.)
The above command may take some time to execute. After completion, you will get something like following on your screen.
Now for checking the container you can use the following command:
[js] lxc-ls –fancy [/js]
It is showing the container in stopped state, for starting the container you will have to run the following command.
[js]lxc-start -n httpContainer -d[/js]
Now you can run the command “lxc-ls –fancy” to check whether container is running or not. If container is running it will show you something like following:
In the above image, it showing container is in running state, and it has also assigned an IP address. In your case it would be different. Container would have internet access, but it can’t be accessible from the internet, because container has associated with a private IP which operates behind an NAT.
Now you can login to the container and install the Apache. For login, you can use the default username password(ubuntu/ubuntu). After login, you will get the regular bash prompt from which you can do almost anything you would do on the host machine. But its possible you won’t find some command on the container because its a minimal installation. For that, you need to install the service by using the apt-get.
First you connect to the container by running the following command:
[js]lxc-console -n httpContainer -t 1[/js]
Now you can install the Apache.
[js]sudo apt-get install apache[/js]
Above command will install the Apache web server and start the service in container. You can access the web server locally or from the host but you can’t access it from the public network as I have mentioned earlier container has private IP which operates behind a NAT. So accessing it from public Network you need to add a rule in host machine.
You will have to run the following command on our host machine which will redirect all the 80 port request to the container.
[js]iptables -t nat -A PREROUTING -i eth0 -p tcp –dport 80 -j DNAT –to 10.0.3.10:80[/js]
Now if you try to access the URL http://our-public-ipaddress it should show you the Apache Default page.
If you want to shutdown or delete the container you can use the following commands :
[js]lxc-stop -n httpContainer[/js]
[js]lxc-destroy -n httpContainer[/js]
So, this was the basic of LXC Container. There is still lot to explore. You can use the following links read more about LXC Containers.https://linuxcontainers.org/ http://docs.oracle.com/cd/E37670_01/E37355/html/ol_about_containers.html