Vagrant, Docker, Virtualenv - the dev trifecta
Build yourself a local Heroku
(This is the first part of a four-part series. Part two is now available - Vagrant as the base OS)
Having your developers write code and run it in an environment that matches your production environment can prevent nasty surprises, but how can do this when your application is hosted on Heroku, and your company doesn't own a single piece of its own IT equipment?
When you are working in a team, on a long-term web app (i.e. a product, not a campaign), having a consistent, predictable, easy-to-build environment within which to write the code is important. Developers coming in to a project need to be able to spin up a working environment, on whatever machine they are working on, without having to spend two days doing it.
For our team, built from freelancers who all have their own equipment and their own ways of working, we have the following requirements for a developer's setup:
- Scripted build
- Predictable and consistent
- Minimal pre-requisites
- Minimal cruft left behind
- Support any editing tool (e.g.
vim
vs.sublime
)
We run our live, staging and test environments on Heroku, so the closer we can get to replicating that locally, the better. What we want is Heroku-in-a-box.
(Just in case you're thinking Dokku, this is not it. Dokku, which is a great project, builds a remote environment into which you can deploy applications via git push, like Heroku. What I am describing is a local environment modelled on Heroku's logical architecture in which you can write-and-run code. A developer's environment, not a development environment, if you like.)
Heroku, at a high level, consists of three main logical elements:
- Heroku itself, the routing 'mesh', file system etc., that underpins the platform
- Add-ons, which provide non-core services via third parties
- Your application, which is built into a 'slug', using a Buildpack
Each of these elements maps to a specific virtualisation technology, and using these to address the individual requirements, you can build an effective local Heroku-like environment:
Vagrant can be used to model the Heroku platform itself, providing a consistent base OS for the new environment.
Docker containers can be used to model add-ons, which are provided to Heroku applications as URLs (+ credentials).
Virtualenv can be used to isolate the application itself, modelling the application slug (cf.
nvm
for Node,rvm
for Ruby).
In subsequent articles I will show how each of these three come together to create a developer environment for building python (spec. Django) web applications that looks like this:
========================================================
Ubuntu 14.04 ........... localhost .......... Vagrant
|
+- nginx ............... localhost:80/443 ... Docker
|
+- APPLICATION ...... localhost:8000 ..... Virtualenv = DEVELOPER
|
+- postgres ...... localhost:5432 ..... Docker
|
+- redis ......... localhost:6379 ..... Docker
|
+- memcached ..... localhost:11211 .... Docker
|
+- elasticsearch . localhost:9200 ..... Docker
========================================================
Making you feel like a superhero.
Part two of this series is now available - Vagrant as the base OS
Making Freelance Work