Docker up, seriously!
If you are feeling the Docker buzz as of late, you may be writing or re-writing your applications to use Docker. If you haven’t written your applications in Docker you are surely missing out.
For example, with an application full of dependencies (services) such as: workers, databases, full-text search (ElasticSearch or Solr), and DNS, Docker makes configuring this type of environment locally seamless that it’s almost too good to be true. You can then just worry about booting up a normal local server and have these dependencies automatically spin up for you at the same time.
Be Warned: Designers will love you.
Now, I’m not going to go over how to configure that just yet as that’s a bigger blog post in itself. However, I wanted to go over what they don’t tell you after this setup and how you can get the most BANG when running your application locally.
I’m a Mac user and so I must use a Virtual Machine (VM) running a docker ready image such as boot2docker. When I started transitioning my Rails apps to use Docker, a ton of tutorials I’d find didn’t mention what specs to allocate my VM with.
For example, in VirtualBox, you could configure it to use the total amount of CPU and enough RAM (4GB was said to be enough). However, I soon had these issues:
- Computer would often get loud (like a plane ready for takeoff)
- Application would sometimes freeze up and I needed to restart the VM
- CPU levels viewed in Activity Monitor would sporadically go through the roof
I then started a mission to attack this problem. First, I found out that mounted volumes on VirtualBox use vboxsf, which is extremely slow when it comes to file syncing from the host (my Mac) to the client (the VM). I read that this caused startup times for code in mounted folders to run 10–20x slower.
Doing some more reading, I read that people have overcome this problem by switching to NFS mount shares for increased speeds. So, I found a project docker-machine-nfs that I installed and soon found the app to run faster.
Listed in its documentation are the options to sync certain folders. This way, you’ll only sync certain folders that your VM truly needs (e.g. ~/.docker, ~/projects). Without this, I was syncing Dropbox, GoogleDrive folders in which case file syncing was always running — not good.
Was everything now gold?
No. I still heard the occasional plane ready for takeoff and while I can live with this happening, sometimes, I still heard it a bit more than that. So, this led me into learning a bit more about my old 2011 MacBookPro and its processor.
I ran into this Github issue where I learned that you need to be careful in the number of CPUs you allocate to your VM. Basically, if you give it too many CPUs, you’ll most likely see some inefficiency which I encountered. So, like the issue suggested, I reduced my CPUs to one, starting small, and I started to see an improvement.
GOOD… but not quite good enough.
Though my computer was quieter, I noticed the app seemed to run a bit slower.
So, this left me asking questions like why did my old non-docker Rails setup seem to run smoothly and rarely sounding like an occasional plane takeoffs? So, I did some more investigation.
On my host computer, I viewed “About my Mac” to gain a deeper insight on my specs. I found out that I have a 2 GHz Intel Core i7 chip.
Okay so what does that mean?
Googling around this, I found that this chip in my 15in MPB is a quad-core processor. Quad means four (duh) so that means there are 4 independent processor “cores” on the single chip. So, then it clicked: Doesn’t that mean the CPUs I allocate to the VM should be four? and four processes could then run in parallel?
Updating to 4 CPUs got me rolling just like the good old days. I then doubled the RAM allocated to the VM and BOOM even faster!
Now, I’m loving my Docker setup and as I learn more about the internals of a computer, I’m confident in not looking back and Docker-izing all my applications because I now know how to carefully calibrate them and get greater efficiency.
To summarize, when Docker-izing your application, don’t forget to:
- Use NFS for file transfers and sync only needed folders (its much faster than the default vboxsf)
- Know how many CPUs your computer runs and set that number in VirtualBox (I recommend googling your processor and finding out if its DualCore(2 CPU) or QuadCore(4 CPU) and setting that number)
- Add more RAM if you can (I recommend allocating at least 1/2 of your computer’s total RAM)
BONUS: On Mac, remove the spotlight indexer from always running. I found this process to always take up some of my computer’s resources and I didn’t even use spotlight. See howTo here: http://osxdaily.com/2011/12/10/disable-or-enable-spotlight-in-mac-os-x-lion/