15

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
2
  • I think the web service needs to know about the BUNDLE_PATH environment variable as well.
    – maletor
    Commented Apr 19, 2016 at 23:59
  • try define in Dockerfile sequence commands WORKDIR and COPY below FROM Commented Nov 13, 2020 at 16:40

1 Answer 1

10

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

2
  • Thanks! So I had to define the variables: ENV GEM_PATH /xxx; ENV GEM_HOME /xxx along with it. I'll try it out sometime, thanks!
    – Jono
    Commented May 16, 2017 at 2:23
  • This was exactly the correct answer for my issue. I never realized that I also had to set the gem path! Thanks!
    – RSmithlal
    Commented Jan 19, 2019 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.