Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installed gems not found by bundler when BUNDLE_PATH changed with Docker

I'm using docker to develop a rails application. The docker file looks like this:

FROM ruby:1.9.3

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev vim

ENV APP_HOME /next-reg
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ENV BUNDLE_PATH /box

ADD . $APP_HOME

RUN gem install gem1.gem  gem2.gem

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock

RUN  bundle install

As you can see I change the BUNDLE_PATH, this is because of an article showing how we can persist gem downloads. So that overtime when the docker cache gets warm, it re-bundles and takes FOREVER.

When I docker build it successfully installs the gems, then it fails to find them on bundle. Could someone give me a hand with persisting gems, installing my own gems, and getting it to work?

Before I changed the BUNDLE_PATH the build worked, it just re bundled frequently without changes to the gem file (because, I guess the docker image cache got warm).

My docker-compose is like this:

db:
  image: postgres
  volumes:
    - ~/.docker-voumes/postgres/data:/var/lib/postgresql/data
# This is to hold and persist ruby gems, referenced in web and in web's dockerfile.
gem_files:
  image: busybox
  volumes:
    - /box

web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/next-reg
  volumes_from: 
    - gem_files
  ports:
    - "3000:3000"
    - "8000:8000"
  links:
    - db
  env_file:
    - .myenv.env
like image 726
Jono Avatar asked Jan 26 '16 17:01

Jono


People also ask

Where are bundler gems installed?

I know that when using gem install , the gem will be stored under /home/username/. rvm/gems/, under which gemset the gem was installed.

How do I install bundles to install missing gems?

In the invoked popup, start typing bundler, select bundle install and press Enter . Select Tools | Bundler | Install from the main menu. Open the Gemfile, place the caret at any highlighted gem missing in the project SDK and press Alt+Enter . Select Install missing gems using 'bundler' and press Enter .


1 Answers

I think that there is a lack of GEM_HOME/GEM_PATH in your code.

GEM_HOME/GEM_PATH will be used by gem install xxx to install gems in a specific folder. BUNDLE_PATH will be used by bundle install to install gems in a specific folder but not by gem install xx

To have a working system you should do :

FROM ruby:1.9.3

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev vim
ENV APP_HOME /next-reg
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ENV BUNDLE_PATH /box
ENV GEM_PATH /box
ENV GEM_HOME /box

ADD . $APP_HOME

RUN gem install bundler
RUN gem install tzinfo -v 1.2.2

COPY Gemfile Gemfile

RUN  bundle install

With this Gemfile :

source 'https://rubygems.org'

gem 'tzinfo', '1.2.2'

Wich will produce :

Step 11/13 : RUN gem install tzinfo -v 1.2.2
 ---> Running in 8a87fa54fa19
Successfully installed thread_safe-0.3.6
Successfully installed tzinfo-1.2.2
2 gems installed
 ---> 3c91d59bde8a
Removing intermediate container 8a87fa54fa19

Step 13/13 : RUN bundle install
 ---> Running in 20f1e4ec93b1
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Fetching gem metadata from https://rubygems.org/...
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...
Rubygems 1.8.23.2 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
Installing rake 12.0.0
Using thread_safe 0.3.6
Using bundler 1.14.6
Using tzinfo 1.2.2
Bundle complete! 2 Gemfile dependencies, 4 gems now installed.
Bundled gems are installed into /box.

As you can see in the result output, the bundle install re-use the preloaded gems from gem install

like image 60
Julien TASSIN Avatar answered Nov 07 '22 21:11

Julien TASSIN