First things first the initial docker image, to do this we need some Interlok config and a Dockerfile. We’ll be working with a the programming classic “Hello World”, to do this in Interlok we’ll be using the jetty-message-consumer consumer and the payload-from-metadata service.
Now that we have that lets build and run it!
Success! What the above commands will do is build an image using the Dockerfile in the current directory and tag it as interlok/hello-world:snapshot and then run it mapping the container port 8081 as localhost:80.
To test it and see whats happened:
To stop either run the following or Ctrl+C in the window:
Scaling with docker-compose
Now we have an image that builds we can use docker-compose to scale it.
To run it:
As we can see from the output we have three running instances of Interlok container. These instances are only reachable from within the containers so we can test they are working using the docker-compose exec command:
To stop either run the following from the same directory or Ctrl+C in the window:
The instances can now be scaled but aren’t reachable, what is needed is load balancer. Using the image dockercloud/haproxy we can update our docker-compose.yaml and introduce one:
The load balancer is containerised version of haproxy, the magic happens when it dynamically generates the contents of the load balancers configuration with a combination of environment variables and the mounted /var/run/docker.sock file.
And to stop again either run the following from the same directory or Ctrl+C in the window:
Taking it Further
Now if we wanted extend the config a little and return the hostname as a part of the “Hello World” message we could and would end up with responses looking something like this:
How this is done isn’t covered here but the solution is available in a github project.
Docker is powerful tool that can be used in development life-cycle or as a part of a production stack. Interlok has working use cases of both, personally I’ve moved most of my development work over to it, creating purpose built images for testing and product demonstrations.
The load balancer dockercloud/haproxy is just another example of clever things docker can do for you.