LXC Containers on Ubuntu

LXC Containers on Ubuntu

Redir is used for port forwarding.

sudo apt-get install lxc redir

Vagrant LXC provider

Note that there is also very interesting Libvirt provider for Vagrant. It’s supports more back ends, but has his quirks. Just watch what’s happening.

Installation of vagrant-lxc is simple as typing:

vagrant plugin install vagrant-lxc

Download basebox (some):

vagrant box add lxc-quantal64 http://dl.dropbox.com/u/13510779/lxc-quantal-amd64-2013-05-08.box

Create Vagrantfile like this:

echo '
Vagrant.configure("2") do |config|
  config.vm.box = "lxc-quantal64"
  config.vm.provider :lxc do |lxc|
    # Same effect as as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox
    lxc.customize 'cgroup.memory.limit_in_bytes', '2048M'
  end
end
' >Vagrantfile

Check possible configuration options here.

Note: There is a set of rake tasks that you can use to build base boxes as needed. Great stuff - rake

vagrant up --provider=lxc

Note: Don’t use sudo for this. You can also set VAGRANT_DEFAULT_PROVIDER environmental variable to lxc in order to avoid typing --provider=lxc all the time.

export VAGRANT_DEFAULT_PROVIDER=lxc

Tips

Containers are, by default, located at /var/lib/lxc/.

List active containers with: sudo lxc-ls --fancy --active Stop container, by name: sudo lxc-stop --name=<somename> To SSH into machine: sudo lxc-console --name=<somename> Remove container: sudo lxc-destroy -f -n <somename>

When there is no Chef in basebox

If there is mising Chef on basebox, just install Omnibus plugin. Support for it is already included in Devbox. It should install latest Chef when needed.

vagrant plugin install vagrant-omnibus

Or you can add line manually:

v1:

config.vm.provision :shell, :inline => "curl -L https://www.opscode.com/chef/install.sh | sudo bash"

v2:

$provision_script= <<SCRIPT
if [[ $(which chef-solo) != '/usr/bin/chef-solo' ]]; then
  curl -L https://www.opscode.com/chef/install.sh | sudo bash
  echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
fi
SCRIPT

config.vm.provision :shell, :inline => $provision_script

v3:

if [[ $(which chef-solo) != '/usr/bin/chef-solo' ]]; then
  wget --quiet https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/11.04/x86_64/chef_10.18.2-2.ubuntu.11.04_amd64.deb
  dpkg -i chef_10.18.2-2.ubuntu.11.04_amd64.deb
  echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
fi

I think that v2 is the best.

Toggling between VirtualBox and LXC

Just rename directory:

mv .vagrant .vagrant-virtualbox

and then set environment variable to LXC.

export VAGRANT_DEFAULT_PROVIDER=lxc
vagrant up

or in one line:

VAGRANT_DEFAULT_PROVIDER=lxc vagrant up

Later on, vagrant will detect the type of active virtual machines. To switch again to VirtualBox, just save your .vagrant dir and restore the old one.

mv .vagrant .vagrant-lxc
mv .vagrant-virtualbox .vagrant

vagrant up

/etc/default/lxc

LXC_BRIDGE=“lxcbr0” LXC_ADDR=“10.0.3.1” LXC_NETMASK=“255.255.255.0” LXC_NETWORK=“10.0.3.0/24” LXC_DHCP_RANGE=“10.0.3.2,10.0.3.254” LXC_DHCP_MAX=“253”

date 01. Jan 0001 | modified 29. Dec 2023
filename: LXC Containers