LXC/LXD as an alternative to Virtualbox for local development

Over the last few days I’ve been experimenting with LXD as an alternative to Virtualbox for local WordPress development tasks.

The advantages are huge:

  1. Speed

LXC/LXD runs at bare metal speeds. Your container will run as fast as your host machine. New containers take around 1 second (!) to launch.

  1. Ease of use

The LXD command line tools are intuitive and useful

  1. Efficient Use of resources

LXC/LXD is much lighter than Virtualbox .

To get started, follow the instructions here to install LXC/LXD on your local machine (sorry OSX users, this stuff is Linux only).

Once you are up and running you will need to import a base image:

 lxd-images import ubuntu --alias ubuntu

Once the image is imported, you can launch your first container

lxc launch ubuntu firstcontainer

Once the container is launched, you may stop it like

lxc stop firstcontainer

…and start it again:

lxc start firstcontainer

To mount a shared host directory, stop the container and first make the directory world writeable on the host (I’m sure there is probably a more graceful way of doing this!)

sudo chmod -R go+w /path/to/shared/directory/on/host

Then issue this command on the host:

lxc config device add firstcontainer webroot disk path=/srv/www/example.com/current source=/home/user/sites/example.com/site

Start the container, and make sure that your .env file is indeed readable from the container:

lxc exec firstcontainer -- /bin/bash
cd /srv/www/example.com/current
cat .env

Now have a look at the output from:

lxc list

(on the host) and you will see the ip address of your new container. Add the development domain (example.dev) to your /etc/hosts file and make sure it is pointing at the correct ip.

Once your dev domain is set up you need to access the container again and add your ssh key to the root user’s ./ssh/authorized_keys file in the container. Once that’s done, you can use ansible to provision the container as normal. Just use the development hosts file and group vars as you would for staging or production.

ansible-playbook -i hosts/development server.yml

I find I need to run the script at least twice to get it to work. For some reason swapon always fails first time but is fine second time.

All being well, you should now be able to work on your local dev site using LXC/LXD instead of Virtualbox.

I’m going to figure out how to do all this with Vagrant, stay tuned, and any help gratefully received.

4 Likes

This is pretty cool, thanks! Would you be interested in expanding on this for a blog post on roots.io? If you don’t want to put it somewhere else that is.

I’ll talk to @benlobovits to see if that’s something we could do (“guest” writers).

@swalkinshaw that would be a huge honour!

Okay we can do this pretty easily. Want to formalize your post a little and post it to a Gist or something? (as Markdown). No rush or anything and I’ll give it an edit after as well.

Yes, I can do that.

Do you think I should do that now or wait until I’ve got vagrant working with LXC/LXD?

Maybe a blog post based on this discourse post can be part one and then I’ll write a follow up when I’ve got it all working with vagrant?

Yeah two parts sounds good.

I didn’t know Vagrant could work with LXC. That sounds amazing.

And since I use Ubuntu for my desktop, count me in as very curious and excited to have bare-metal speed :joy:

I’ve got two containers set up and running Roots based projects. LXC/LXD almost seem too good to be true: fast, stable and extremely lightweight. I just noticed that if you shutdown the host machine without stopping the containers first, LXC automatically brings them back up again on boot. Cool :smile:

Am I correct in assuming that to bypass VirtualBox you have to be using Ubuntu on your development machine, vs. Mac OS X or Windows?

1 Like

Correct. Check out the full blog post here: https://roots.io/linux-containers-lxd-as-an-alternative-to-virtualbox-for-wordpress-development/

Thanks.

FWIW I actually came to the forum to ask after reading the full post. If it was explicitly stated then I missed it, sorry.

Note that Docker no longer uses LXC. In 2014 Docker started using their own libcontainer instead of LXC.
reference: https://github.com/docker/libcontainer

Not just Ubuntu, any modern Linux should do the trick. Ubuntu is the reference platform though as Canonical are driving development of LXD.

Nice post!
I’d like to know if you are using something to automate and easily reproduce containers, something like Vagrant maybe.

I’m working on it. Been away for a few weeks but back now and will have something to show soon, hopefully.

In the meantime, I find it’s pretty simple to use LXC/LXD without Vagrant.

Once you have the shared folders configured and SSH working pretty much everything else is handled by the trellis ansible scripts.

I’ve become used to using this setup now and Virtualbox just seems so sluggish in comparison.

1 Like

I am using linux mint, just started testing lxd. I have installed lxd as instructed by this post.
But while trying to add official repository for images i get following message.

lxc remote add images images.linuxcontainers.org

No command ‘lxc’ found, did you mean:
Command ‘axc’ from package ‘afnix’ (universe)
Command ‘llc’ from package ‘llvm’ (universe)
Command ‘lpc’ from package ‘lprng’ (universe)
Command ‘lpc’ from package ‘lpr’ (universe)
Command ‘lpc’ from package ‘cups-bsd’ (main)
Command ‘lc’ from package ‘mono-devel’ (main)

Is there something I should be doing with lxc? To my knowledge lxc is already installed on my machine along with lxd.

Hope to get help, Thank you.

Check lxc is installed:

which lxc

If not you should install it.

thanks for the reply, i checked it earlier and they are installed.

apt-get install lxc
Reading package lists… Done
Building dependency tree
Reading state information… Done
lxc is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 374 not upgraded.

apt-cache policy lxc
lxc:
Installed: 2.0.0~rc1-0ubuntu1~ubuntu14.04.1~ppa1
Candidate: 2.0.0~rc1-0ubuntu1~ubuntu14.04.1~ppa1
Version table:
*** 2.0.0~rc1-0ubuntu1~ubuntu14.04.1~ppa1 0
500 http://ppa.launchpad.net/ubuntu-lxc/lxd-stable/ubuntu/ trusty/main amd64 Packages
100 /var/lib/dpkg/status
1.0.8-0ubuntu0.3 0
500 http://archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
1.0.7-0ubuntu0.7 0
500 http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
1.0.3-0ubuntu3 0
500 http://archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

What happens if you try running lxc as root?

It give same message as above.

lxc
No command ‘lxc’ found, did you mean:
Command ‘axc’ from package ‘afnix’ (universe)
Command ‘llc’ from package ‘llvm’ (universe)
Command ‘lpc’ from package ‘lprng’ (universe)
Command ‘lpc’ from package ‘lpr’ (universe)
Command ‘lpc’ from package ‘cups-bsd’ (main)
Command ‘lc’ from package ‘mono-devel’ (main)