Solution To Manage Gems Effectively

Learn to manage gems effectively using Gem Cache Volume.

Using a Gem Cache Volume

As we have seen, the key problem is that Docker’s image building, akin to building a machine from scratch, is at odds with the caching of gems with Bundler. What if, instead of fighting the image build process, we bypassed it?

We know how volumes provide persistent file storage that is separate from the container filesystem, and we can use this to solve our thorny problem.

Here’s the gist. By mounting a volume in the directory where Bundler installs our gems, we can execute Bundler commands to populate and manage the gems on this volume, which effectively becomes a local gem cache. Remember that a mounted volume overlays, and is separate from, the container’s filesystem. Its files persist beyond the life cycle of the container itself.

View in practice

Let’s see how this works in practice. First, we need to configure Bundler to use an explicit, known directory for installing gems to .../gems, say. We do this by setting the BUNDLE_PATH environment variable. Let’s update our Dockerfile as follows:

Get hands-on with 1200+ tech skills courses.