LXC (Linux containers) is an open source Operating system level virtualization method which works same as docker and provides containers as an instance of an image which controls over same host kernel.Linux_containers. So in this post we would see how to create Containers

Linux kernel provides cgroups function that provides us a power to work on proper control on the resource in terms of limitation and prioritization without sharing a resource with hypervisor layer and starting new virtual machines. So this providing a complete isolation only for an application, there is no other thing than an application on the same Host. But as we have complete isolated setup we can transfer and share an application with its minimum size and dependencies.

So with this method, we have complete virtual machine instance with its own process and network space as compared to complete virtual machine it’s quite resourcing efficient.

Same kind of concept comes with Docker as well, earlier Docker also used lxc as execution driver. But now they have their own drivers and working alone.

Today, we going to work on Linux containers (LXC), which is also good options for a virtual machine, where you can publish your applications and transfer them in very light and easy way as compared to Virtual Machines.

let’s start it, As compared to Docker, Linux containers don’t have any hub to download and publish their images. They are using some template scripts located /usr/share/lxc/templates, which sometimes I think a better option as compared to Docker hub.

When we try to create any container, it makes its image on spot with help of those script under directory /var/cache/lxc and further launch its container under /var/lib/lxc directory. So let’s start how things work in Linux containers.

installation of Containers

Linux containers in CentOS6. For this setup, we are using CentOS6.9

[root@srv3 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)

LXC packages are already included in CentOS repositories.

#yum install lxc* libvirt

I always recommend installing packages of lxc. Although lxc and lxc-template is important for functions. Libvirt is also another essential package for LXC function.

Now start lxc and libvirt service on your host.

[root@srv3 ~]# /etc/init.d/lxc restart
Stopping LXC containers:                                   [  OK  ]
Starting LXC autoboot containers:                          [  OK  ]

[root@srv3 ~]# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-2.6.32-696.6.3.el6.x86_64
--- 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 namespace: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: /usr/bin/lxc-checkconfig: line 114: [: -ge: unary operator expected
/usr/bin/lxc-checkconfig: line 114: [: -gt: unary operator expected
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled

--- Checkpoint/Restore ---
checkpoint restore: missing
File capabilities: /usr/bin/lxc-checkconfig: line 148: [: -gt: unary operator expected

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

You can’t start any Linux container without libvirt service. it good to start libvirt before creating and Linux Container.

# /etc/init.d/libvirtd start

# chkconfig libvirtd on

Creation of Linux Containers

#lxc-create -n centos -t centos
#lxc-info --name centos
Name:           centos
State:          STOPPED

#lxc-start -d --name centos #This will start Container in background
#lxc-info --name centos     #Container information
Name:           centos
State:          RUNNING
PID:            19413
CPU use:        0.29 seconds
BlkIO use:      0 bytes
Memory use:     2.75 MiB
Link:           vethD6VM1K
 TX bytes:      510 bytes
 RX bytes:      94 bytes
 Total bytes:   604 bytes

#lxc-console --name centos  #Container console

It will show login screen, Here you can login. For me sometimes password didn’t work which is stored in “/var/lib/lxc/Centos/tmp_root_pass” if same happened with you, change it to with below command

#chroot /var/lib/lxc/Centos/rootfs passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

You will get below kind of output of centos container.

#lxc-console --name centos

Connected to tty 1
Type  to exit the console,  to enter Ctrl+a itself

CentOS release 6.9 (Final)
Kernel 2.6.32-696.1.1.el6.x86_64 on an x86_64

centos login: root
Last login: Tue Jul 18 11:04:53 on lxc/tty1
[root@centos ~]#

For exit from console press ctl+a q.