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:
- 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.
- Ease of use
The LXD command line tools are intuitive and useful
- 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.