Difference between revisions of "LXC"

From Jon's Wiki
(New page: These instructions assume Ubuntu 12.04 LTS. == Install and set up LXC container == First, install LXC and create a base container, then clone a project container from it. Here's roughly ...)
 
Line 3: Line 3:
 
== Install and set up LXC container ==
 
== Install and set up LXC container ==
  
First, install LXC and create a base container, then clone a project container from it. Here's roughly what I had to do to set up LXC nicely.
+
In sum, we install LXC and create a base container, set it up nicely, then clone project containers from it as required. Here's roughly what I had to do to set up LXC nicely.
  
 
  sudo apt-get install lxc
 
  sudo apt-get install lxc

Revision as of 01:58, 8 February 2013

These instructions assume Ubuntu 12.04 LTS.

Install and set up LXC container

In sum, we install LXC and create a base container, set it up nicely, then clone project containers from it as required. Here's roughly what I had to do to set up LXC nicely.

sudo apt-get install lxc

Create a base container to serve as a template

sudo lxc-create -t ubuntu -n base
sudo lxc-start -n base

Log in with the default account, (username ubuntu, password ubuntu), then add yourself as a user with sudo:

sudo adduser username
sudo adduser username sudo

This installs some nifty default guff, fixes the probably broken UTC timezone, and fixes locales BEFORE you install things that expect UTF-8 locales, but in particular PostgreSQL (otherwise you end up with SQL_ASCII encoding and nobody wants that):

sudo apt-get install git bash-completion language-pack-en openssh-server
sudo dpkg-reconfigure tzdata

Set up a SSH shortcut for containers

Back on your host workstation, set up an SSH shortcut. If you add the following to your ~/.ssh/config, you will be able to ssh to your containers nice and easily:

# LXC containers
Host *.lxc
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  ProxyCommand nc $(host $(echo %h | sed "s/\.lxc//g") 10.0.3.1 | tail -1 | awk '{print $NF}') %p

Then one can ssh to the containers by appending .lxc to the container name:

ssh myproject.lxc

and then schlepp your handy stuff over:

scp -rp ~/.bashrc ~/.vim* ~/.gitconfig base.lxc:
scp ~/.ssh/authorized_keys base.lxc:.ssh/authorized_keys

Clone a new project container from the template

Now you should have a useful base container, so clone it for your nifty-kitten project and start it (most usefully in a screen session):

sudo lxc-shutdown -n base
sudo lxc-clone -o base -n nifty-kitten
screen
sudo lxc-start -n nifty-kitten
[Ctrl-a] [d] to detach