How to create and manage LXC containers on Ubuntu ?, Create lxc containers in ubuntu
Install LXC on Ubuntu
$ 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
After installing the LXC tool, you will find that the default bridge interface (lxcbr0) of LXC has been automatically created (configured in/etc/lxc/default. conf ).
$ Ifconfig lxcbr0
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
Find the available lxc template in the/usr/share/LXC/templates directory.
$ ls /usr/share/lxc/templates
1.Create a configration file, named lxc_common.conf:
lxc.network.type = vethlxc.network.flags = uplxc.network.name = eth0lxc.network.link = lxcbr0
2.Change mirrors:
sudo vim /usr/share/lxc/templates/ubuntu
Change this to the faster mirror server you know:
MIRROR=http://archive.ubuntu.com/ubuntuSECURITY_MIRROR=http://security.ubuntu.com/ubuntu
3. 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 precise -f lxc_common.conf -t ubuntu -- -r precise
4.Configure the network, sudo vim /etc/default/lxc-net
, change the configs if you like, I have uncomment this line:
LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
5.Use a fixed ip address for “precise” container, sudo vim /etc/lxc/dnsmasq.conf
:
Dhcp-host = precise, 10.0.3.2
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 precise
Restart lxc-net:
$ sudo service lxc-net restart
Start the lxc container:
$ sudo lxc-start -n precise
Stop the lxc container:
$ Sudo lxc-stop-n precise
For details, see LXC installation and configuration.