In my previous article, I talked about the confusing world of virtualization. To the uninitialized, it can be really confusing which technology or tool to use.
But to sum it all up, if you are running a data center, then KVM/XEN are your best bet, if you are system admin or Devops guy who wishes to have isolated or network server environments, then Linux containers are your thing, but if you are developer wanting to ship your app from development to production, then docker is the tool for the job.
I mainly fall in the Devops area, so my point of interest is with Linux containers and to some extend Docker containers. So this post, I'll introduce you to some commands and how to setup Linux containers or LXC on any Linux distro.
Installing and Creating Linux Containers on Ubuntu/Debian
#install lxc sudo apt-get update sudo apt-get install lxc #create a new container lxc-create -t ubuntu -n c1 #list containers lxc-ls --fancy NAME STATE IPV4 IPV6 AUTOSTART ----------------------------------------------------- c1 RUNNING 10.0.3.21 - NO c2 STOPPED - - NO #start a container sudo lxc-start -n c1 → use the -d (daemon option ) to daemonize the container #attach to a running container from host lxc-attach -n c1 #stop a container lxc-stop -n c1 #clone a container lxc-copy -n old-container -N new-container #destroy a container lxc-destroy -n container-name
Tip: Creating a Linux container takes a hours on slow internet connections. It took about an hour on my localhost while in the cloud it took about 3 minutes!
Actually I had issues with this on my localhost. So I created a lxc container on a cloud box, zipped the container folder in /var/lib/lxc/container-name and downloaded it. Then moved it to /var/lib/lxc/container-name on my localhost.
LXC Config file location
Container config files is usually located at /var/lib/lxc/$container-name/config. Here you can make several tweaks to your container such as specifying static ip, whether it should autostart among other settings.
Here's my sample config file
# Template used to create this container: /usr/share/lxc/templates/lxc-ubuntu # Parameters passed to the template: # For additional config options, please look at lxc.container.conf(5) # Common configuration lxc.include = /usr/share/lxc/config/ubuntu.common.conf # Container specific configuration lxc.rootfs = /var/lib/lxc/c1/rootfs lxc.mount = /var/lib/lxc/c1/fstab lxc.utsname = c1 lxc.arch = amd64 # Network configuration lxc.network.type = veth lxc.network.flags = up lxc.network.link = lxcbr0 lxc.network.hwaddr = 00:16:3e:fb:89:1b ########my config lxc.network.ipv4 = 10.0.3.100/24 10.0.3.255 lxc.network.ipv4.gateway = auto lxc.start.auto = 1 lxc.start.delay = 15 lxc.start.order = 10 lxc.mount.entry = /home/oquidave/lxc_mount share none bind 0 0
Mount host directory to an LXC container
Sometimes you want to have a shared directory between containers or between the host and the lxc containers, here's how you do it;
On the host machine, create the directory you wish to mount on the LXC container.
mkdir /media/data/share && chmod 7777 /media/data/share
Create directory in lxc container
Edit lxc config file on host
Add the following entry;
lxc.mount.entry = /media/data/share share none ro,bind 0.0
Subscribe to David Okwii dev blog
Get the latest posts delivered right to your inbox