How to create and manage LXC containers on Ubuntu?

Source: Internet
Author: User

How to create and manage LXC containers on Ubuntu?

Ubuntu plugin container

Although the concept of containers was introduced more than a decade ago to securely manage shared host hosting environments such as FreeBSD prison ), however, Linux, such as LXC and Docker, has recently entered the mainstream because of the increasing need to deploy applications for cloud computing. Despite the media's interest in Docker over the past few days, it has received strong support from major cloud service providers such as Amazon AWS and Microsoft Azure, as well as release providers such as RedHat and Ubuntu, however, LXC is actually one of the earliest container technologies developed for the Linux platform.

Docker create ubuntu 16.04 container

What are the benefits of Docker/LXC if you are an ordinary Linux User? Well, containers are actually a good way to switch between various releases almost instantly. Assume that your current desktop system is Debian, and you need the stability of Debian. At the same time, you want to play the latest Ubuntu game. Then, you don't have to bother entering the Ubuntu partition Through Dual Boot, or start the Ubuntu virtual machine that occupies a large amount of resources. You just need to enable an Ubuntu container immediately, and everything is done.

Ubuntu container image

Even without the advantages of Docker, what I like about LXC containers is that LXC can be managed by the libvirt interface, but not Docker. If you have been using libvirt-based management tools such as virt-manager or virsh before, you can use the same tools to manage LXC containers.

In this tutorial, I will focus on the command line usage of the standard LXC container tool and demonstrate how to create and manage LXC containers from the command line on Ubuntu.

Install LXC on Ubuntu

To use LXC on Ubuntu, install the LXC user space tool as follows.

$ sudo apt-get install lxc

After installation, run lxc-checkconifg to check whether the Linux kernel supports LXC. If everything is enabled, the kernel is ready to support LXC.

$ lxc-checkconfig

Docker ubuntu container

After installing the LXC tool, you will find that the default bridge interface lxcbr0 of LXC has been automatically created and configured in/etc/lxc/default. conf ).

Run ubuntu container on windows 


After the LXC container is created, the window interface is automatically connected to the bridge, so that the container can be connected to the outside world.

Create LXC container

To create an LXC container in a specific target environment such as Debian Wheezy 64-bit, you need a corresponding LXC template. Fortunately, the LXC user space tool on Ubuntu comes with a series of pre-prepared LXC templates. You can find the available lxc templates in the/usr/share/LXC/templates directory.

$ ls /usr/share/lxc/templates


Docker ubuntu container run

The LXC template is actually a script used to create a container for a specific Linux environment. You need to use this template when creating an LXC container.

For example, to create an Ubuntu container, run the following command:

$ sudo lxc-create -n-t ubuntu



By default, it creates a minimal Ubuntu installation system with the same version number and architecture as the local host, in which case it is Saucy salamand13.10) 64-bit.

If you want to create a Ubuntu container of any version, you only need to pass the release parameter. For example, to create a Ubuntu 14.10 container:

$ sudo lxc-create -n-t ubuntu -- --release utopic

It downloads and verifies all the packages required by the target container environment. The entire process may take several minutes or longer, depending on the container type. So please be patient.



After downloading and verifying a series of packages, the LXC container image is finally created. You will see the default logon information for use. The container is stored in/var/lib/lxc/<container-name>, and the root file system is located in/var/lib/lxc/<container-name>/rootfs.

All packages downloaded during LXC creation are cached in/var/cache/lxc, so that it takes no time to create additional containers using the same LXC template.

Now let's take a look at the LXC container list on the host:

$ sudo lxc-ls –fancy

Name state IPV4 IPV6 AUTOSTART

------------------------------------

Test-lxc STOPPED--NO

To start a container, run the following command. The-d option starts the container as a daemon. Without this option, you will be directly connected to the console after starting the container.

$ sudo lxc-start -n-d

After the container is started, check the container status again:

$ sudo lxc-ls –fancy

Name state IPV4 IPV6 AUTOSTART

-----------------------------------------

Lxc RUNNING 10.0.3.55-NO

As you can see, the container is RUNNING) and has been assigned an IP address.

You can also confirm that the container interface, such as vethJ06SFL, is automatically connected to the internal bridge lxcbr0 of LXC), as shown below.

$ brctl show lxcbr0



Manage LXC containers

Now that we know how to create and start the LXC container, let's take a look at how we can handle the running container.

First, we want to access the container console. To do this, type the following command:

$ sudo lxc-console -n



Type <Ctrl + a q> to exit the console.

To stop and destroy a container:

$ sudo lxc-stop -n$ sudo lxc-destroy -n

To clone an existing container into another container, run the following commands:

$ sudo lxc-stop -n$ sudo lxc-clone -o-n

Troubleshooting

If you encounter LXC errors, the following are some key points for troubleshooting.

1. You cannot create an LXC container and the following error occurs.

$ 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

This means that you run the latest LXC, but use the older libcgmanagerlibcg manager ). To solve this problem, you need to update the libcg manager.

$ sudo apt-get install libcgmanager0

Http://xmodulo.com/lxc-containers-ubuntu.html.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.