Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active October 7, 2017 16:51
Show Gist options
  • Save cloudnull/9045e1aa44011fccbf62 to your computer and use it in GitHub Desktop.
Save cloudnull/9045e1aa44011fccbf62 to your computer and use it in GitHub Desktop.
How to create a container using LXC >= 1.0

How to setup a host to use LXC

Create the bridge where all LXC devices will be attached

auto eth2
iface eth2 inet manual

auto br-eth2
iface br-eth2 inet static
        address 10.51.50.1
        netmask 255.255.255.0
        bridge_ports eth2
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0
        dns-nameservers 69.20.0.164 69.20.0.196

If not deploying on Ubuntu 14.04 add this PPA to the system to provide for LXC 1.x

# Add the LXC Stable back ports repo
add-apt-repository -y ppa:ubuntu-lxc/stable

# Update
apt-get update

Install the LXC packages

# Install LXC
apt-get -y install lxc python3-lxc lxc-templates liblxc1

Create a default LXC configuration file to build with

# Update the lxc-rpc.conf file
cat > /etc/lxc/lxc-rpc.conf <<EOF
lxc.start.auto = 1
lxc.group = rpc

# Default LXC network
lxc.network.type = veth
lxc.network.name = eth0
lxc.network.link = lxcbr0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:xx:xx:xx

# Create a veth pair within the container
lxc.network.type = veth
# Network device within the container
lxc.network.name = eth1
# Host link to attach to, this should be a bridge
lxc.network.link = br-eth2
# Hardware Address
lxc.network.hwaddr = 00:16:3e:xx:xx:xx
# enable the device on boot
lxc.network.flags = up
EOF

Setup LVM if possible

if [ -e "/dev/xvde" ];then
  apt-get update && apt-get install lvm2
  parted -s /dev/xvde mktable gpt
  parted -s /dev/xvde mkpart lvm 0GB 100%
  pvcreate /dev/xvde1
  vgcreate lxc /dev/xvde1
fi

How to create a container

Build the first container

# Note: $CONTAINER_NAME is a string
# Note: $CONTAINER_PACKAGES is a comma separated list
lxc-create -n $CONTAINER_NAME \
           -t ubuntu \
           -f /etc/lxc/lxc-rpc.conf \
           -- \
           --release $DISTRO_NAME \
           --user openstack \
           --password secrete \
           --packages $CONTAINER_PACKAGES

Before you start the container assign it an IP address. Change the eth1 address for your environment.

cat > /var/lib/lxc/$CONTAINER_NAME/rootfs/etc/network/interfaces <<EOF
# The loopback network interface
auto lo
iface lo inet loopback

# Label public
auto eth0
iface eth0 inet dhcp

# Bridged Network
auto eth1
iface eth1 inet static
    address 10.51.50.$ADDRESS
    netmask 255.255.255.0
    gateway 10.51.50.1
EOF

Now start the container

lxc-start -d --name $CONTAINER_NAME

Make sure SSH starts up by default

lxc-attach --name $CONTAINER_NAME <<EOL
    update-rc.d ssh defaults
    service ssh restart
EOL

Now your ready to get building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment