Open In App
Related Articles

How to Manage Linux Containers using LXC

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

LXC is a technology for creating light weighted containers on your system. This technology is able to run on more than one Linux host. It is just like its counterparts i.e the other hypervisors like KVM and Xen. Unlike complete virtualization, containerization does not provide the user with complete isolation but the amount of load that is exerted on the Host OS is considerably less. 

In this article, we will see the Management of Linux containers with the help of LXC, the Linux distribution that will be used in Ubuntu.

Installing LXC on the system

Use the below command to install LXC on your system:

sudo apt-get install lxc

 

Unfortunately, the above command does not download all the templates, so we might run into an error named No such file or directory – bad template while creating the container. To make sure we have the templates installed run the below command:

sudo apt-get install lxc-templates

 

Creation, Listing, login, and stopping of the container:

Step 1: Creation

A new container can be created by using the below command, we will be creating a sample container named sample1 which will be based on an ubuntu template :

sudo lxc-create -t  <name_of_template> -n <name_of_container>

 

This process takes a while because there are a lot of packages to be collected and the creation of a container is also time taking.

Step 2:  Listing the container(s)

Use the below command to list the available containers on your system:

sudo  lxc-ls

 

By default the user created is ubuntu and the password is also ubuntu.

 

To see the complete details about the container use the below command:

sudo lxc-info -n <name_of_container>

 

Step 3:  Starting the container

To start the container use the following command:

sudo lxc-start -n <name_of_container>
 or 
sudo lxc-start -d -n <name_of_container> (To start the container in a detached mode)

 

To verify the running status of the container use the below command:

sudo lxc-info -n <name_of_container>

 

Step 4:  Logging into the container

To log in to the container we take the help of the lxc-console command:

sudo lxc-console -n <name_of_container>

 

To stop the container use the below command:

sudo lxc-stop -n <name_of_container>

 

Verify it:

sudo lxc-info -n <name_of_container>

 

Step 5:  Freezing and Unfreezing

With the help of the lxc-freeze command we can freeze the containers:

sudo lxc-freeze -n <name_of_container>

 

Confirm whether the container is stopped or not:

You can see the FROZEN state.

To unfreeze the frozen container use the below command:

sudo lxc-unfreeze -n <name_of_container>

 

Step 6:  Cloning the container and its power off

Before we clone a container make sure to stop it using the command discussed above, then type the below command to make a clone of it. The name of our cloned container will be sample2.

Note: Earlier the command had lxc-clone instead of lxc-copy, lxc-clone is now deprecated.

sudo lxc-copy -n <old container> -N <new container>

-n —-> old name,  -N—-> new name

 

The clone of the container can also be seen along with the existing container.

Step 7: Powering off the container

To power off the container use the below command:

sudo poweroff

 

Verify it once the container is stopped:

 

Step 8: Take snapshots of a container and its deletion

To take a snapshot of a container use the lxc-snapshot command:

sudo lxc-snapshot  -n <name_of_container>

 

To delete a container use the below command:

sudo lxc-destroy -n <name_of_container>

 

Management of LXC Containers using a web panel:

Some people find working with the command line a bit tedious, this method is just for them. By installing the web panel of LXC one can manage the containers with the help of GUI.

Note: For installing the web panel you must be a root user.

sudo su
wget http://lxc-webpanel.github.io/tools/install.sh -O - | bash

 

The user interface can be accessed at the  Url: http:/your_ip_address:5000/ by using the user id and password which by default are admin and admin.

 

Go to the site and enter the details:

 

You have successfully entered the GUI panel, now you can maintain the containers from the web panel easily.


Last Updated : 01 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads