"Containers" are used to secure the host environment, a concept that existed a decade ago (for example, FreeBSD's jail virtualization technology), but until recently, as the deployment of cloud architectures became more demanding, containers such as LXC and Docker were the focus of attention. Of course, thanks to the powerful backers of mainstream manufacturers (such as Amazon's main push AWS, Microsoft's main push Azure, the release such as Red Hat, Ubuntu, etc.), Docker has been placed under the spotlight of the media, in fact, Docker's so-called "container" technology is provided by LXC.
You're just an ordinary Linux user, what's the benefit of DOCKER/LXC? Containers can migrate your applications between different Linux distributions. Imagine the scene: The release you're using is Debian, you like its stability, and you want to play the latest Ubuntu game, you don't need to install a dual system on your computer and restart into Ubuntu, and you don't need to run a resource-intensive Ubuntu virtual machine on Debian, You just need to simply generate an Ubuntu container that's enough.
Aside from the benefits of Docker, let's talk about the benefits of the LXC container: I can use the interface provided by Libvirt to manage LXC, which have nothing to do with Docker. If you have a management tool based on Libvirt libraries (such as Virt-manager and Virsh), you can use them to manage the LXC container.
In this tutorial, I'll just introduce the command-line operations of the standard LXC container management tool to teach you how to create and manage LXC containers under Ubuntu.
Install LXC under Ubuntu
Use the following command to install LXC in user-state tools:
The code is as follows:
$ sudo apt-get install LXC
Then check to see if the current kernel supports LXC. If all results are "enable", the kernel supports:
The code is as follows:
$ lxc-checkconfig
After the LXC tool is installed, you can see that LXC automatically creates a bridging network card (LXCBR0, which can be set in/etc/lxc/default.conf).
When you create the LXC container, its web interface is automatically linked to the bridging network card, and the container can communicate with the outside world.
Creating a LXC Container
To create LXC containers in a given environment (such as the Debian wheezy 64-bit), you need a corresponding LXC template. Fortunately, the tools provided by LXC integrate a set of ready-made LXC templates that you can find in the/usr/share/lxc/templates directory.
The code is as follows:
$ ls/usr/share/lxc/templates
A LXC template is essentially a script that creates a container in a specified environment. When you create a LXC container, you need to use them.
For example, if you want to create a new Ubuntu container, use the following command:
The code is as follows:
$ sudo lxc-create-n- t ubuntu
By default, this command creates a minimal Ubuntu environment in which the version number is the same as your host, and my side is a "lively newt" (version number 13.10) and 64 bits.
Of course you can also create any version you like, just add a version parameter to the command. For example, create a container for Ubuntu 14.10:
The code is as follows:
$ sudo lxc-create-n- t ubuntu----release utopic
This command downloads the packages that are installed in the specified environment and creates a new container. The whole process takes a few minutes and is related to the type of container, so you may have to wait.
After downloading all the packages, the LXC container mirror is created and you can see the default login interface. The container is placed under the/var/lib/lxc/< container name > directory, and the container's root file system is placed under the/var/lib/lxc/< container name >/rootfs directory.
The packages downloaded during the creation are saved under the/VAR/CACHE/LXC directory and can save a lot of download time when you want to build a different container.
Use the following command to look at all the LXC containers on the host:
The code is as follows:
$ sudo lxc-ls--fancy
NAME State IPV4 IPV6 AUTOSTART
------------------------------------
TEST-LXC STOPPED--NO
Use the following command to start the container. The parameter "-D" opens the container as a background process. If you do not specify this parameter, you can close the container's running program directly on the console interface (LCTT CTRL + C key combination).
The code is as follows:
$ sudo lxc-start-n- D
After opening the container, look at the status:
The code is as follows:
$ sudo lxc-ls--fancy
NAME State IPV4 IPV6 AUTOSTART
-----------------------------------------
LXC RUNNING 10.0.3.55-no
The container state is "in run" and the container IP is 10.0.3.55.
You can also see that the container's network interface (like my VETHJ06SFL here) is automatically connected to the LXC internal Bridge (LXCBR0):
The code is as follows:
$ brctl Show Lxcbr0
Manage LXC containers
We've learned how to create and launch LXC containers and now look at how to play a running container.
First step: Open the container console:
The code is as follows:
$ sudo lxc-console-n
Use the "Crtl+a Q" key combination to exit the console.
To stop and delete a container:
The code is as follows:
$ sudo lxc-stop-n
$ sudo lxc-destroy-n
To copy the container, use the following command:
The code is as follows:
$ sudo lxc-stop-n
$ sudo lxc-clone-o - n
Problems
This section mainly introduces the problems you have encountered during the use of LXC.
The following error was encountered while creating the LXC container:
The code is as follows:
$ sudo lxc-create-n test-lxc-t ubuntu
Lxc-create:symbol Lookup error:/usr/lib/x86_64-linux-gnu/liblxc.so.1:undefined symbol:cgmanager_get_pid_cgroup_ Abs_sync
The reason for the error is that you run the latest LXC, but it relies on a libcgmanager version that is older and incompatible. You can fix the problem by upgrading the Libcmanager:
The code is as follows:
$ sudo apt-get install Libcgmanager0