Mounting Local Gems with Docker / Docker-Compose

October 8, 2021

Sometimes when you're developing a Ruby Gem for a containerized application, you want to use your local copy so you can have changes reflected without pushing up a new commit and calling `bundle update my_gem`. Fortunately, it's really very easy to do this.

Edit docker-compose.yml

First, edit your `docker-compose.yml` file and find the volumes for your web app:

services:
  web:
    volumes:
      # Here, use the absolute path of your external code folder:
      - /home/my_username/Code/local-project:/app-path/vendor/local-project

A few notes:

  • Here, we're assuming that your gem is checked out locally at `/home/my_username/Code/local-project`, and within that folder you'll have a `local-project.gemspec` file;
  • We're also assuming that your Ruby or Rails application is mounted at `/app-path`;
  • You don't need to create a `VOLUME` instruction in your Dockerfile; the above will work just fine to mount it;
  • Even if `/app-path` is a mounted folder (as it very well might be in development), you can still mount a folder within it.

Edit Gemfile

Secondly, edit your Gemfile and make sure that your `gem` instruction points to the local copy:

# Comment this one out:
# gem "local-project", "~> 2.0.0"
# And add
gem "local-project", path: "vendor/local-project"

Then be sure to `bundle`, and that's it! Don't forget to switch back to the remote repository (bumping the version, probably!) before you deploy.