Docker on Google Compute Engine – a Hello World with OpenCV

Docker seems to strike a great middle-ground between complete-but-full-blown VM’s (a la VirtualBox or VMWare) and error-prone-but-super-lightweight setup/production scripts. Its architecture built on layers offers an interesting amount of granularity and I felt like I needed to build my own little Hello World.

docker-top-logo

I chose to build a Docker file for a build image [1] with OpenCV, a computer vision library, and specifically with its Java binding and test its deployment on Google Compute Engine [2]. While very popular, OpenCV isn’t particularly easy to set up and thus makes for a nice use-case for something not completely trivial as a random ‘apt-get install’. Note this is a trusted build, hosted on index.docker.io as a link to the Dockerfile on github and built by the Docker Index system.


After building a binary for OpenCV 2.4.7 together with its Java support (which is not part of the default build and somewhat tedious to generate), I created this simple image that simply places the compiled bits in /opt and places a simple face recognition demo application in the home directory. You’ll note that the build includes the install of an updated version of libc6-dev to satisfy Java OpenCV shared libraries. Docker nicely hides this mess to the user of the image.

Of course, there also needs to be a proper Java installed. This is done by building the OpenCV demo image from another trusted build (alexismp/debian-openjdk) which itself builds off of debian wheezy. As the name implies it installs OpenJDK (7) and configures JAVA_HOME and PATH.

Finally, my OpenCV demo image provisions ANT which is used to run the sample code with a trivial “ant build” command in the demo directory (/home/faceDetect).

small-computeenginesmall-storageRunning this software stack on Google Compute Engine is as easy as pulling the build from Docker’s repository from a Docker-enabled virtual machine [1], running docker with shared directories, and moving the result to Google Cloud Storage :

$ sudo docker pull alexismp/opencv-demo-java7-trusted
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alexismp/opencv-demo-java7-trusted latest 4a9f88286d2e 36 hours ago 783.7 MB (virtual 5.123 GB)
$ sudo docker run \
    -v /tmp:/root \
    -t -i alexismp/opencv-demo-java7-trusted \
    bash
% cd home/faceDetect/
% ant
...
% cp *.png /root/
% exit

$ gsutil mb gs://face-detect/
$ gsutil cp /tmp/*.png gs://face-detect/

The OpenCV test uses face detection (not recognition! :) and creates a PNG images boxing the faces it has detected. Note how simple it is to use the Cloud Storage gsutil command-line to create a new bucket and copy the generated image which at this point is available from a RESTful API.

faceDetection2

Being on the Google network with my Compute Engine instance made the pull operations pretty painless with great download speeds and the backup to Google Storage plain trivial. I also used a simple persistent disk with docker installed and configured which is booting in just a few seconds.

The next step is to integrate this functionality and make it available as a generic service, FDaaS, Face Detection as a Service! This will require writing a front-end or an offering an API and tweaking the Java code to deal with more flexible input/output.


Using Docker on the Mac currently required me to setup Vagrant, VirtualBox and Docker itself, something between dependency hell and Russian dolls but I’m told I should really look at boot2docker (which also sounds like an interesting solution for docker in production).

Interested in Docker? Their Getting Started page is pretty good. Start there and move to the equally good documentation when needed. Just remember that this is still a dot (0.x) release and officially not suited for production.


[1]: if you find the Docker vocabulary a bit confusing, you’re not alone. Check out this nicely written post : http://blog.thoward37.me/articles/where-are-docker-images-stored/

[2]: Docker on Google Compute Engine – http://docs.docker.io/en/master/installation/google/

Author: alexismp

Google Developer Relations in Paris.