Setting up gem caching server to speed up docker builds

First up, why do we even need a gem caching server? Once you install a gem it does get cached in your system by default. Method mentioned in this post can be useful if you are using vagrant or docker to do bundle installs a lot of times. For example in my case every time I tried to build my docker image the bundle install command used to take a lot of time because in every build it used to download all the gems again.

Solution for this problem is gemstasm. Gemstash is a gem caching server which you can run on your machine. The idea is to request the gems from this caching server rather than directly from rubygems website. This way if you request for a gem which is already in cache then you don’t have redownload it again. For this to work you have to configure gemstash as a mirror server but that doesn’t involve a lot of configuration.

Firstly, install gemstash on your host machine and start it.

gem install gemstash --no-ri --no-rdoc
gemstash start

This will start a gem caching server on port 9292

Now in your Dockerfile you have to configure this gemstash server as a mirror for downloading gems. Add this line to your Dockerfile.

RUN bundle config mirror.

Now any bundle install will use your docker cache. This saves up a lot of time if you need to build and rebuild containers. Note that the IP address is the IP address of your docker0 interface. This might be different on your system. If you are doing your builds on Vagrant or virtual box then replace the with the IP address of your vbox bridge interface.

NOTE: This will not work on Mac OS.