Deploying prototype analytics applications with Docker
I recently caught wind of Docker -- a modern virtualisation technology -- and after a little research and investigation, I've set out to discover what all the buzz is about. Docker, which describes itself as "a platform for developers and sysadmins to develop, ship and run applications" combines a container-based virtualisation engine and a community platform for application stack sharing.
One of the many promises of Docker is the lightweight nature of applications deployed with the engine. Compare to a more traditional hypervisor-based approach where one might spin up a virtual appliance for their database server, and a second for their webserver, Docker claims to remove the need for a Guest OS for each application, and instead serve applications in a manner not too far removed from Apple's approach to sandboxing apps in iOS (and more recently, OS X).
Sandboxed, or "containerised", applications share a single Host OS, but otherwise run standalone with their own dependencies, which means that application stacks can be built, tested and maintained more easily as individual components. Managing dependencies at the application level, like traditional virtualisation, also means avoiding lib version conflicts -- great!
The one major caveat is that Host OS's are limited to Linux environments for the time being -- though apparently the upcoming Windows Server 2015 will support Docker for Windows-based application environments.
With all that in mind, onto our first recipe...
A simple Docker salad with Shiny dressing
Preparation time 1 hour, serves 1 Shiny web-app
Ingredients
VirtualBox 4.3.24 (Windows, OS X, Ubuntu 64-bit)
Ubuntu 14.04 AMD64 Desktop (ISO) (We should ideally be using a server edition of Linux here, but I only had the desktop image handy at the time of writing.)
An Internet connection
Steps
Download the ingredients listed above and install VirtualBox.
Using the Ubuntu ISO, create an Ubuntu VM to host our Docker install. I’d recommend allocating at least 2GB of RAM. Follow through all the prompts, for this tutorial I’ve created a machine called docker with admin username deliciousdata. Click through the installation prompts, grab a coffee and come back in about 15 minutes.
Open a Terminal session and install Docker:
sudo apt-get update sudo apt-get install docker.io
You can test your install by running:
sudo docker run -i -t ubuntu /bin/bash
This will download the Ubuntu base image and open a bash shell within the container. Go ahead and run the following command and marvel at your creation:
echo 'Hello, world!' exit
A quick search on the Docker Registry reveals a Shiny Server dockerfile. Shiny is an R package released by the creators of RStudio for web application development in R. It makes building pretty great data visualisations a much simpler process.
To run a Shiny container, run the following command (this may take a while depending on your Internet connection, time for another coffee?):
sudo docker run --rm -p 3838:3838 rocker/shiny
Docker will run the rocker/shiny Dockerfile, install all dependencies, and fire up Shiny Server on port 3838. Once completed (Docker will stop displaying status updates), you should be able to open FireFox within your Ubuntu VM and see Shiny running at http://localhost:3838/.
To stop the Docker container, run:
sudo docker ps
and locate the Container ID (in my case, 191baf3d0d19), and then run:
sudo docker stop 191baf3d0d19
(http://localhost:3838/ should no longer respond.)
The next step is to customise the Shiny web app with one of the examples from RStudio's GitHub.
sudo docker run --rm -p 3838:3838 -v /srv/shiny-server/:/srv/shiny-server/ rocker/shiny
This will create the /srv/shiny-server/ directory on the Host OS (if it does not already exist), and map it to the folder within the Shiny Server container. In a new Terminal, run the following:
cd /srv/shiny-server/ sudo wget https://raw.githubusercontent.com/rstudio/shiny-examples/master/008-html/server.R sudo mkdir www cd www sudo wget https://raw.githubusercontent.com/rstudio/shiny-examples/master/008-html/www/index.html
Go ahead and visit http://localhost:3838/ once more and see the Shiny app you just concocted with Docker in all of its glory.
That completes this tutorial. Look out for further posts about my journey with Docker, and an upcoming post about customising HTML webapps with RShiny.
Tomorrow I'll be attending the March Docker Meetup at Atlassian in Sydney, where Tim Robinson (founder of Volt Grid) will be presenting "Intro to Docker in Production (from an Ops perspective)" and Ruben Rubio Rey (CTO of manageacloud.com) will present his talk on "How to use configuration management systems to manage Docker containers".










