当BUNDLE_PATH使用Docker更改时,Bundler找不到已安装的gem

时间:2016-01-26 17:31:55

标签: ruby-on-rails docker dockerfile docker-compose

我使用docker开发rails应用程序。 docker文件如下所示:

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

正如您所看到的,我更改了bundle_path,这是因为an article显示了我们如何坚持下载宝石。因此,当docker缓存变热时会超时,它会重新捆绑并取消FOREVER。

当我docker build成功安装宝石时,它无法在捆绑上找到它们。有人可以给我一个持久的宝石,安装我自己的宝石,让它工作吗?

在我更改BUNDLE_PATH构建工作之前,它只是经常捆绑而不更改gem文件(因为,我想Docker图像缓存变热了)。

我的docker-compose是这样的:

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

1 个答案:

答案 0 :(得分:3)

如果有人需要回答:

我认为您的代码中缺少GEM_HOME / GEM_PATH。

gem install xxx将使用GEM_HOME / GEM_PATH在特定文件夹中安装gem。 捆绑安装将使用BUNDLE_PATH在特定文件夹中安装gem,但不能通过gem install xx

要建立一个有效的工作系统:

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

使用这个Gemfile:

source 'https://rubygems.org'

gem 'tzinfo', '1.2.2'

将产生:

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.

正如您在结果输出中看到的那样,bundle install重新使用gem install

中预加载的宝石