The first question would have raised in the mind What is LXC?
It is the userspace interface for the Linux kernel containment features.
Through simple steps, we will be to create and manage the application containers.
It is open source, licensed under GNU LGPLv2.1+, standard 2-clause BSD and GNU GPLv2. We can install the LXC directly from the Linux distribution repository. It will install all the required packages into the machine. To install the LXC you should have rights in the particular machine. Run the below command from your terminal,
sudo apt-get install lxc
Now that LXC is installed into your system, you can verify the installation by running the given below command,
lxc-checkconfig
It will give you the below information
Kernel configuration not found at /proc/config.gz; searching…
Kernel configuration found at /boot/config-3.19.0–25-generic
— — Namespaces — -
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled
— — Control groups — -
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled
— — Misc — -
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
We will be creating the Linux containers as unprivileged in this article, for privilledged may look forward on my future articles. You have installed only in the root, you will be able to create the container only when you in the root. If other users tried to create the container directly it will fail. To allow any particular user to create privilege first we need to make sure the user has uid and gid map defined in /etc/subuid and /etc/subgid andwe need to add the user to the file /etc/lxc/lxc-usernet . By default, your user isn’t allowed to create any network device on the host, to change that, add
your-username veth lxcbr0 10
This means that “your-username” is allowed to create up to 10 veth devices connected to the lxcbr0 bridge.
And now, create your first container with:
lxc-create -t download -n my-container
The download template will show you a list of distributions, versions, and architectures to choose from. A good example would be “ubuntu”, “bionic” (18.04 LTS) and “i386”. or you can give the below command directly
lxc-create -t ubuntu -n my-container
It will install the latest ubuntu directly from the Ubuntu distributions.
A few seconds later your container will be created and you can start it with:
lxc-start -n my-container -d
You can then confirm its status with either of:
lxc-info -n my-container
lxc-ls -f
And get a shell inside it with:
lxc-attach -n my-container
Stopping it can be done with:
lxc-stop -n my-container
And finally removing it with:
lxc-destroy -n my-container
Hope it helps. Feel free to comment below if you find any discrepancies. For more information check out the official website Linux Containers.
Cheers,
Hari