Difference between revisions of "LXC"

From Jon's Wiki
Line 50: Line 50:
 
  sudo lxc-start -n '''nifty-kitten'''
 
  sudo lxc-start -n '''nifty-kitten'''
 
  '''''[Ctrl-a] [d]''''' ''to detach''
 
  '''''[Ctrl-a] [d]''''' ''to detach''
 +
 +
ssh nifty-kitten.lxc

Revision as of 02:53, 8 February 2013

LXC, or Linux Containers, is the successor to VServer and is now in the main kernel; most modern distributions should have it. The LXC Ubuntu Server Guide is very good and contains pretty much all you need to know. A lot of work has been done on 12.04 to make it very simple to get started. 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
ssh nifty-kitten.lxc