LXC (Linux Containers) is an operating-system-level virtualization environment for running multiple isolated Linux systems (containers) on a single Linux control host. Here is a quick start guide for getting started with lxc on Ubuntu Linux.
- First install lxc package
$ sudo apt-get install lxc
-
Create a container (ubuntu trusty release)
$ sudo lxc-create -t download -n ubuntu-test -- --dist ubuntu --release trusty --arch amd64
-
List existing containers.
$ sudo lxc-ls --fancy NAME STATE IPV4 IPV6 AUTOSTART ------------------------------------------- ubuntu-test STOPPED - - NO
-
Start the container
$ sudo lxc-start --name ubuntu-test --daemon
-
In case you need to stop and or destroy container
$ sudo lxc-stop -n ubuntu-test $ sudo lxc-destroy -n ubuntu-test
-
List existing containers again
$ sudo lxc-ls --fancy NAME STATE IPV4 IPV6 AUTOSTART ------------------------------------------------ ubuntu-test RUNNING 10.0.3.54 - NO
-
Display info about container using name
sudo lxc-info --name ubuntu-test Name: ubuntu-test State: RUNNING PID: 7629 IP: 10.0.3.54 CPU use: 0.64 seconds BlkIO use: 1.62 MiB Memory use: 7.79 MiB KMem use: 0 bytes Link: veth4WTBQP TX bytes: 3.25 KiB RX bytes: 3.38 KiB Total bytes: 6.64 KiB
-
Install python on the container
$ sudo lxc-attach --name ubuntu-test -- sudo apt-get install python
-
Run python hello world on the container
$ sudo lxc-attach --name ubuntu-test -- python -c "print \"Hello\""
-
Run shell on container
$ sudo lxc-attach --name ubuntu-test root@ubuntu-test:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var