How to Launch LXC Linux Containers with LXC Commands

In the previous LXC article, we explained how to install and configure LXC linux containers.

In this tutorial, we’ll explain how to create a new Linux container, start the container, and login to the LXC virtual console to use the new container.

LXC Containers are a very quick way to create a psuedo virtual environment.

 

For demonstration, we’ll be creating a CentOS LXC virtual container in this tutorial, but you can create virtual container for pretty much any Linux distro that you want.

1. LXC Container Templates

Linux Containers LXC by default provides container templates for several popular linux distros. The following are some of the LXC templates that you can use immediately.

  • CentOS
  • Ubuntu
  • Fedora
  • OpenSUSE
  • Gentoo
  • Debian
  • Oracle Linux
  • ArchLinux

All available LXC templates are located under /usr/local/share/lxc/templates directory.

# ls -1 /usr/local/share/lxc/templateslxc-alpinelxc-altlinuxlxc-archlinuxlxc-busyboxlxc-centoslxc-cirroslxc-debianlxc-downloadlxc-fedoralxc-gentoolxc-openmandrivalxc-opensuselxc-oraclelxc-plamolxc-sshdlxc-ubuntulxc-ubuntu-cloud

2. Create a Container using lxc-create

To create the container, use lxc-create command as shown below.

In the following command:

  • –t option indicates the template that is used to create the container. In this example, we are using lxc-centos template to create a CentOS container.
  • –n option indicates the container name

In this example, this will create the CentOS container with centos minimal install.

This will download all the packages that are required to run the CentOS minimal (for example, approximately around 140 packages), and install them as part of the MyCentOSContainer1 container

# lxc-create -n MyCentOSContainer1 -t /usr/local/share/lxc/templates/lxc-centos/usr/local/share/lxc/templates/lxc-centosHost CPE ID from /etc/system-release-cpe: cpe:/o:centos:linux:6:GAdnsdomainname: Unknown hostChecking cache download in /usr/local/var/cache/lxc/centos/x86_64/6/rootfs ... Downloading centos minimal .........  Installing : libgcc-4.4.7-16.el6.x86_64       1/142   Installing : setup-2.8.14-20.el6_4.1.noarch   2/142   Installing : filesystem-2.4.30-3.el6.x86_64   3/142 ...  Verifying  : gzip-1.3.12-22.el6.x86_64        139/142   Verifying  : mingetty-1.08-5.el6.x86_64       140/142   Verifying  : libxml2-2.7.6-20.el6_7.1.x86_64  141/142   Verifying  : 1:findutils-4.4.2-6.el6.x86_64   142/142 ...Download complete.Copy /usr/local/var/cache/lxc/centos/x86_64/6/rootfs to /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs ... Copying rootfs to /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs ...

3. Root Password for the LXC Container

The root password for the LXC container is stored under: /usr/local/var/lib/lxc/{container-name}/tmp_root_pass

In the above example, for the LXC container that we just created, temporary root password is under the following:

# cat /usr/local/var/lib/lxc/MyCentOSContainer1/tmp_root_passRoot-MyCentOSContainer1-pf7qMB

You can change the root password of your container at any time, even if your container is turned off by using chroot from the control host as shown below.

# chroot /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs passwdChanging password for user root.New password: Retype new password: passwd: all authentication tokens updated successfully.

4. Start the LXC Linux Container using lxc-start

Once the container is created, use lxc-start as shown below to start your container.

# lxc-start -n MyCentOSContainer1lxc-start: cgfs.c: handle_cgroup_settings: 2077 Device or resource busy - failed to set memory.use_hierarchy to 1; continuingCentOS release 6.6 (Final)Kernel 2.6.32-431.el6.x86_64 on an x86_64MyCentOSContainer1 login: init: rcS main process (8) killed by TERM signalEntering non-interactive startupBringing up loopback interface:               [  OK  ]Bringing up interface eth0:Determining IP information for eth0... done.  [  OK  ]Starting system logger:                       [  OK  ]Mounting filesystems:                         [  OK  ]Generating SSH2 RSA host key:                 [  OK  ]Generating SSH1 RSA host key:                 [  OK  ]Generating SSH2 DSA host key:                 [  OK  ]Starting sshd:                 [  OK  ]CentOS release 6.6 (Final)Kernel 2.6.32-431.el6.x86_64 on an x86_64MyCentOSContainer1 login:

If you get the following “container failed to start” error message, then something is wrong in your configuration.

# lxc-start -n MyCentOSContainer1lxc-start: lxc_start.c: main: 344 The container failed to start.lxc-start: lxc_start.c: main: 346 To get more details, run the container in foreground mode.lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --logfile and --logpriority options.

If you get the following, “failed to attach to bridge”, “failed to create netdev”, “failed to create the network” error message, then bridge adapter is not configured properly on your system.

To find-out exactly what is wrong with your configuration, run the LXC container in the foreground using the -F option as shown below.

Check the ifcfg-br0 settings that we mentioned in our earlier LXC Linux Container setup article, to make sure you have the bridge configuration setup properly and working.

# lxc-start -F -n MyCentOSContainer1lxc-start: conf.c: instantiate_veth: 2796 failed to attach 'veth8VPW29' to the bridge 'lxcbr0': Operation not permittedlxc-start: conf.c: lxc_create_network: 3079 failed to create netdevlxc-start: start.c: lxc_spawn: 935 failed to create the networklxc-start: start.c: __lxc_start: 1192 failed to spawn 'MyCentOSContainer1'lxc-start: lxc_start.c: main: 344 The container failed to start.lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --logfile and --logpriority options.

5. View Container Info using lxc-info

To display detailed information about your container, use lxc-info command as shown below.

# lxc-info -n MyCentOSContainer1Name:           MyCentOSContainer1State:          RUNNINGPID:            7696IP:             192.168.100.101CPU use:        1.61 secondsBlkIO use:      32.00 KiBMemory use:     2.63 MiBLink:           vethLNI1UH TX bytes:      1.98 KiB RX bytes:      46.08 KiB Total bytes:   48.06 KiB

6. Stop LXC Linux Container using lxc-stop

To stop your LXC linux container use lxc-stop as shown below, and specify the container name that you like to stop.

# lxc-stop -n MyCentOSContainer1

7. Clone an Existing Container using lxc-clone

Cloning is useful when you want to take a snapshot of the old container before you make any changes to the configuration on the container.

You can use this as a simple backup method for container configuration.

Cloning the container can be done using lxc-clone command as shown below. In this case, the new container name is MyCentOSContainer2

# lxc-clone -o MyCentOSContainer1 -n MyCentOSContainer2Created container MyCentOSContainer2 as copy of MyCentOSContainer1

8. Delete an Existing Container using lxc-destory

To delete an existing container, use lxc-destroy command as shown below.

# lxc-destroy -n MyCentOSContainer2

9. Connect to LXC Linux Container Console

To connect to the console of the container, use the following lxc-console command.

# lxc-console -n MyCentOSContainer1Connected to tty 1Type Ctrl+a q to exit the console, Ctrl+a Ctrl+a to enter Ctrl+a itselfCentOS release 6.6 (Final)Kernel 2.6.32-431.el6.x86_64 on an x86_64MyCentOSContainer1 login:

10. Change LXC Configurations using lxc-config

To change the LXC configuration, you can use lxc-config command.

First, use lxc-config -l option, which will just display all the available configurations as shown below.

# lxc-config -llxc.default_configlxc.lxcpathlxc.bdev.lvm.vglxc.bdev.lvm.thin_poollxc.bdev.zfs.rootlxc.cgroup.uselxc.cgroup.pattern

Next, use lxc.default_config option as shown below to view information about each configuration file and its associated parameters.

# lxc-config lxc.default_config/usr/local/etc/lxc/default.conf# cat /usr/local/etc/lxc/default.conflxc.network.type = vethlxc.network.link = virbr0lxc.network.flags = up

The following command will display the LXC patch where all the containers are stored.

# lxc-config lxc.lxcpath/usr/local/var/lib/lxc

As we see below, we see the two containers that we created under this directory.

# cd /usr/local/var/lib/lxc# lsMyCentOSContainer1  MyCentOSContainer2

Under the container directory, you’ll see the config file, which contains all the basic configuration information for that particular container as shown below.

# cd /usr/local/var/lib/lxc/MyCentOSContainer1# vi configlxc.network.type = vethlxc.network.link = virbr0lxc.network.hwaddr = fb:bb:f2:87:e8:17lxc.network.flags = uplxc.rootfs = /usr/local/var/lib/lxc/MyCentOSContainer1/rootfslxc.include = /usr/local/share/lxc/config/centos.common.conflxc.arch = x86_64lxc.utsname = MyCentOSContainer1

>

You may also like...