Skip to content

Instantly share code, notes, and snippets.

@keeb-zz
Created July 23, 2013 19:35
Show Gist options
  • Save keeb-zz/6065461 to your computer and use it in GitHub Desktop.
Save keeb-zz/6065461 to your computer and use it in GitHub Desktop.
The power of Docker+Hipache - Immediate Test Environments, even with Static Port Forwarding

I do most of my development on a remote server or on a local virtual instance. It just makes more sense, being able to work in a portable environment and pick up where I leave off lets me worry less about environment and more about the problem at hand.

One of the problems with using a technology like VirtualBox in combination with Docker is that Docker containers are truly portable and should expose a random port chosen by the host while VirtualBox port forwarding is static. You could, for every container, create a new port forwarding rule in your VirtualBox configuration, but that requires you change host configuration to be able to run your application. Not ideal and certainly not portable.

Enter hipache. Hipache is the best way to configure reverse proxies. In this example, hipache is the entrypoint for all web traffic on the host. Running hipache in a container just like any other service allows for ultimate flexibility.

Vagrantfile

Vagrant is a cool way of abstracting VM semantics and spinning up base OS's to test and develop in. The following Vagrantfile doesn't do anything interesting but create a 13.04 version of Ubuntu. The part we care most about for this article is the config.vm.forward_port configuration, which tells the VM to forward port 80 to 8080.

Vagrant::Config.run do |config|
  config.vm.box = "raring"
  config.vm.box_url = "http://cloud-images.ubuntu.com/raring/current/raring-server-cloudimg-vagrant-amd64-disk1.box"
  config.vm.forward_port 80, 8080
  config.vm.share_folder("vagrant-root", "/vagrant", ".")
  config.vm.customize ["modifyvm", :id, "--memory", 2048]
end

##The Dockerfile

from ubuntu:12.10
maintainer Nick Stinemates

run apt-get install -y python-setuptools
run easy_install pip
add . /website
run pip install -r /website/requirements.txt

expose 5000

cmd ["python", "website/server.py"]

Orchestration

This script is where the magic happens and glues all of the technologies together.

#!/bin/bash

hipache_container=$(docker ps | grep hipache | cut -d" " -f1)
if [ -z $hipache_container ]; then echo "hipache not running"; exit 1; fi

hipache_redis_port=$(docker port $hipache_container 6379)
hipache_bridge=$(docker inspect $hipache_container | grep Bridge | cut -d":" -f2 | cut -d'"' -f2)
ip=$(/sbin/ifconfig $hipache_bridge | sed -n '2 p' | awk '{print $2}' | cut -d":" -f2)

docker build -t keeb/bd:latest .

container=$(docker run -d -t keeb/bd)
container_port=$(docker port $container 5000)

#delete whatever was there last time
redis-cli -p $hipache_redis_port keys "frontend:bd.localhost" | xargs redis-cli -p $hipache_redis_port del

#create the new entries
redis-cli -p $hipache_redis_port rpush frontend:bd.localhost bd-site
redis-cli -p $hipache_redis_port rpush frontend:bd.localhost http://$ip:$container_port/

Conclusion

Running the shell script above provides a fully functional test environment in 10 seconds. It can be rerun as many times as you wish without having to change a thing, and makes your latest changes always available on http://bd.localhost:8080/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment