It has been a long time since I blogged. In the past 24hrs I started using Docker for development on my Mac. I wanted to share some tips Ive learned in the past few hours, including tips for port forwarding and getting things running on my mac.
Firstly, why Docker? Docker is a cloud hosting service for VMs, essentially. I’ve been interested in trying Docker for a while and an opportunity presented itself this week. I was working on a project using Solr so I installed it via brew on my mac. I then tried running through Solr’s quickstart and my machine barfed
As this is a pretty typical experience, initially I was undeterred and went about searching for some answers. The more I dug into the problem the worse it became! “Oh you just need to modify the Solr script to startup with a path to these jars.” said a post referring to an earlier version of Solr than what I had installed. “You need to use this ExtractionRequestHandler in the solrconfig.xml.” Sounds great! Except that the directory structure in Solr had changed over the latest versions and it seems that isnt actually the problem since that didnt work either. On top of this, my coding partner, on the other hand, had installed solr from the tgz, not via brew, and was having none of these problems.
Stop the insanity!
Docker to the rescue! By using containers to develop this app, I can avoid several unsavory things
- I can fiddle with installations on a virtual machine instead of cluttering up my personal computer
- By creating a container for the project anyone who develops on it can use the EXACT SAME environment
- I can create an Ubuntu VM, and not have to deal with OS X specific issues (well, kind of)
Installing Docker on Mac
To run Docker on mac you want to get Boot2Docker. Its this easy:
$ brew install boot2docker
$ boot2docker init
$ boot2docker up
boot2docker will then give you a path to the vm to set the DOCKER_CERT_PATH.
You can install docker just as easily
$ brew install docker
Docker has an excellent online tutorial for learning the ropes.
Customizing a VM
I started with the ubuntu:latest image, which gives you a bare bones ubuntu installation. No git, nano, etc. You have to install what you need, and ONLY what you need! Keep in mind VM sizes can get large quickly. Docker provides ways to link containers so you can (and should!) containerize liberally. I didnt do that in this case, since I was just poking around for the first time and trying to get things setup.
I found this tutorial really helpful for getting some basics installed.
To get Solr up and running I had to install Java. This tutorial was helpful for getting that done, I just installed the default-jre and default-jdk and Solr ran fine for me.
Docker supports Dockerfiles, which are basically makefiles for creating an image. I found this Dockerfile for creating a Solr image really helpful. After running through the steps to install Solr I was able to get it up and running
So I have my mac, running boot2docker, running docker, running Solr… how do I get to see the Solr web console on my mac?
Port forwarding is kind of like magic, its true.
The first item of information you need is… what IP address is boot2docker using?
$ boot2docker ip
The VM’s Host only interface IP address is: 192.168.59.103
Excellent! This is the IP address to point your browser at to see whats running in Docker
When starting up docker, I can use the -p option to specify port forwarding, with the syntax -p HOSTPORT:CONTAINERPORT i.e.
$ docker run -i -t -p 8001:8983 gizm00/solr /bin/bash
I chose to forward the Docker port 8983 because this is where the Solr quickstart tutorial starts the Solr server.
Once inside my docker I can start Solr on this port (see above) and voila!
Very exciting! In addition to forwarding the port Solr is running on, I also have a webapp I want to forward as well. You can forward multiple named ports using -p again, i.e.
$ docker run -i -t -p 8001:8983 -p 8002:8000 gizm00/solr /bin/bash
So lets recap. I went from struggling with platform-specific issues trying to get a package to run on my personal hardware to having a Docker vm up and running, with my packages of interest AND app installed, with the ports forwarded to my mac in about half a day’s worth of work. AND, now I have my VM uploaded to the Docker registry so I can share it with my project partner. I cant tell you how great it is to know we wont have to debug environment specific issues while we are working on this project together! Whew!
If you would like to check out the VM it is here Ill warn you that its fairly hefty (1Gb). “Refactoring” my container strategy is on the todo list for sure 😉