Using local gems within a Docker-Compose setup

I use Docker for most of my code which is awesome, but as always sharing things between the host/container can be tricky. For instance, sometimes I want to use a local gem in some app:

gem "rails", path: "../my-own/rails"

This would work locally but this will obviously not work within Docker, because at build time (when bundle [install] is ran) the volumes are not mounted.

Here's a semi-gross but powerful hack:

  1. build your image ;
  2. log into your container and print the path of the gem:

    docker-compose run --rm your-container sh
    
    root@58153cf221f2:/app# bundle info rails --path
    /usr/local/bundle/gems/rails-8.0.1
    
  3. now hack that path into your compose setup:

    services:
      web:
        # [...]
        volumes:
          - "../my-own/rails:/usr/local/bundle/gems/rails-8.0.1"
    

Et voilĂ , you're good to go and hack away.

PS: note that Rails is actually a special case because it hosts a bunch of sub-gems (action-X, railties) so you'll have to go one level deeper to make it work:

services:
  web:
    # [...]
    volumes:
      - "../my-own/rails/railties:/usr/local/bundle/gems/railties-8.0.1"
Last updated: 2025-01-07 Tue 16:39