Overview
tl;dr
This setup lets you route through a running fig project to an app you start on your machine outside of docker
Motivation
When developing locally you frequently need to run dependencies of your application locally. This can be problematic if you are running a different operating system than your hosting environment. Frequently database versions or dependencies on other installed and/or running applications can cause the ‘It works in my machine!’ problem. This project allows developers to run a copy of production while running a local copy of any work in progess applications. Running a fig project like this allows for a very fast development feedback cycle.
Required Tools to Install
docker, fig, and boot2docker
Docker
We use docker to run all of our databases, applications, and wiring. Installation documents here.
Fig
Fig allows you to start up docker containers and cordinate how they run. Installation documents here.
boot2docker
Boot2docker enables non-linux operating systems (Mac and Windows) to run docker as if it was native. To install boot2docker follow the docs here. Note that you need have version 1.3.x installed for the volume mounting to work correctly.
Fig Project Setup
We will need to setup a dns server to ensure that our docker containers and local system resolve our development urls correctly. This will make all of our docker containers run with the –dns flag and our local system use the dns docker container to resolve specific domain names. (e.i. dev.nicgrayson.com and local.nicgrayson.com) We will also setup a load balancer using haproxy to route traffic through our docker containers.
DNS Server
Using the nicgrayson/boot2docker-dns docker image we will add our needed local development domains via docker volumes. You will need to create two zone files like this:
Then you will need a file named customzones
to load these zones:
Note that the ip 192.168.59.103
is the boot2docker ip address and 192.168.59.3
is the ip of your local machine from a boot2docker container. This is how the traffic is routed to and from running docker containers.
Configure Your Computer’s DNS
On a Mac you will need to create a file for each domain you wish to use the docker dns for.
Load Balancer
This haproxy container is the magic that makes all of this work so well. It will check if a local app is running. If so it will direct traffic to this app, otherwise it will send traffic to the running docker container. To set this up we will use the dockerfile/haproxy image and a volume mount. This volume needs to contain an haproxy.cfg file. Here is an example:
This config will check for a running api sinatra app and route to the local system or the docker container.
Fig.yml File to Put it All Together
Example Project Output
fig up
without the local app started:
log when app starts up:
You can see in the output above that the local copy of the app reported down then the load balancer detected that the app started. This causes all traffic info api.dev.nicgrayson.com to instead route to the local, outside of docker, instance of the application. This project can be found on my Github https://github.com/nicgrayson/docker-local-development
Take Aways
Running a local copy of your production services should be easy and fast. Swaping out one piece of your environment to work on locally is fantastic. Sharing a fig project with your team ensures that everyone is using the same dependencies to develop.