Deploying linked apps to Dokku using CORS.
Dokku is an open source Heroku clone which I am rather fond of. It is a short script that combines the functionality of git receiver, heroku buildpacks and docker containers. It has a plugin system that lets you add functionality e.g. linking to database or cache containers.
Docker is a wrapper script around Linux Containers (LXC) which allows separate Linux environments to run in a virtual container. Network Address Translation (NAT) is used to forward ports from the host to ports in the container (e.g. 41653 --> 8000).
In order to route requests to the correct containers Dokku uses Nginx to forward requests from a particular hostname on port 80 to a local port (e.g. 41653) where our deployed application is running inside a docker container and listening for requests.
This functionality is built-in to Dokku as the nginx-vhosts plugin (inside /var/lib/dokku/plugins/nginx/vhosts). The plugin is responsible for checking the value of VHOST (at /home/dokku/VHOST) to get the hostname for the Dokku instance then creates an nginx.conf file (at /home/dokku/<deployed_app>/nginx.conf). Each time a project is deployed the process is run which means you can't modify the Nginx conf files manually as they would be destroyed and rebuilt on each deploy.
For a project I've been working on there is a separate backend (built in Django) and frontend (Backbone) which need to communicate. Formerly we were using a hack to allow both the frontend and the backend to have the same origin however if you only need to support newer browsers (http://caniuse.com/cors) then you can use Cross-origin resource sharing (CORS) which requires headers to be added in the Nginx config.
If you are feeling lazy you can just run the following commands to enable CORS and then just redeploy your application. Lovely.
cd /var/lib/dokku/plugins
git clone https://github.com/chrisfranklin/nginx-vhosts.git
All this does is add CORS headers to the generated nginx.conf files (see /var/lib/dokku/nginx-vhosts/post-deploy for more info) which allows you to have a separate backend domain (e.g. backend.hostname.com) and frontend domain (frontend.hostname.com) and communicate between the two.
In another post I'll detail how to setup SSL with Dokku in a painless fashion and then we will go over linking external services in more detail.