LXC (Linux Containers) – quick start tutorial on Ubuntu

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.

lxc-linux-container-tutorial

  1. First install lxc package
    $ sudo apt-get install lxc
    
  2. Create a container (ubuntu trusty release)

    $ sudo lxc-create -t download -n ubuntu-test -- --dist ubuntu --release trusty --arch amd64
    
  3. List existing containers.

    $ sudo lxc-ls --fancy
    NAME         STATE    IPV4  IPV6  AUTOSTART  
    -------------------------------------------
    ubuntu-test  STOPPED  -     -     NO  
    
  4. Start the container

    $ sudo lxc-start --name ubuntu-test --daemon
    
  5. In case you need to stop and or destroy container

    $ sudo lxc-stop -n ubuntu-test
    $ sudo lxc-destroy -n ubuntu-test
    
  6. List existing containers again

    $ sudo lxc-ls --fancy
    NAME         STATE    IPV4       IPV6  AUTOSTART  
    ------------------------------------------------
    ubuntu-test  RUNNING  10.0.3.54  -     NO 
    
  7. 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
    
  8. Install python on the container

    $ sudo lxc-attach --name ubuntu-test -- sudo apt-get install python
    
  9. Run python hello world on the container

    $ sudo lxc-attach --name ubuntu-test -- python -c "print \"Hello\""
    
  10. 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
    
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments