Progmar Marcin Załęczny

Language:

LXC in 64-bit Ubuntu 16.04

LXC - is a LinuX Containers that allow to create virtual environments for working. Each container uses the same kernel as host system.

Install following packages with their dependencies: sudo apt install lxc lxctl libvirt-bin

Check if you have enabled control groups in the kernel: $ lxc-checkconfig The result of the above command should look something like that:

Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-4.4.0-57-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
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled
FUSE (for use with lxcfs): enabled

--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled

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

Creates container of name ubuntu_lxc with Ubuntu system in the same version as in host system: sudo lxc-create -n ubuntu_lxc -t ubuntu Option -n specifies the name of the container being created and option -t specifies template (above it is a template for Ubuntu system) that is used to create new container (available templates are located in following directory: /usr/share/lxc/templates).

Creates container of name trusty_lxc with Ubuntu 14.04 system (Trusty Tahr) in 64-bit architecture: sudo lxc-create -n trusty_lxc -t ubuntu -- -r trusty -a amd64 After -r option we specify release name of the system (--release ex. trusty) and after -a option we specify architecture of a system (ex. amd64).

Lists existing containers: sudo lxc-ls

Runs container with specified name: sudo lxc-start -n ubuntu_lxc -d

Displays information about specified container (for example if it is running now): sudo lxc-info -n ubuntu_lxc

Stops specified container: sudo lxc-stop -n ubuntu_lxc

After launching the container we can attach to its shell by issuing following command: sudo lxc-console -n ubuntu_lxc Caution! The command above can display only following result:

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

and there will be no possibility to connect to its shell. In the case you should make sure that IP of the container (displayed by command lxc-info) is not blocked by the host firewall.
Default credentials for container's shell of Ubuntu system are:
login: ubuntu
password: ubuntu
This credentials are printed out at the end of process of the container creation. When your job in the container is done, you can logout by pressing <Ctrl+D> and leave container by pressing <Ctrl+a q>.

Clones specified container (the container being clonning have to be stopped): sudo lxc-clone -o name_of_container_to_clone -n name_of_new_container

Removes specified container: sudo lxc-destroy -n nazwa_pojemnika

Freezes container: sudo lxc-freeze -n container_name

Unfreezes container - makes it is running again: sudo lxc-unfreeze -n container_name

Displays all active containers: sudo lxc-ls --active

Displays all stopped containers: sudo lxc-ls --stopped

Displays IP address of specified container: sudo lxc-info -i -n container_name

Displays PID of container's process: sudo lxc-info -p -n container_name

Runs application inside the container: sudo lxc-attach -n container_name --command ex. container's upgrade: sudo lxc-attach -n ubuntu_lxc -- apt-get -qq update
sudo lxc-attach -n ubuntu_lxc -- apt-get -qq -y upgrade
or checks if ssh server is running inside the container: sudo lxc-attach -n ubuntu_lxc -- systemctl status sshd.service

Waits for container of specified name is getting RUNNING state: sudo lxc-wait -n ubuntu_lxc -s 'RUNNING'

Waits for container of specified name is getting STOPPED state: sudo lxc-wait -n ubuntu_lxc -s 'STOPPED'