当 BUNDLE_PATH 使用 Docker 更改时,捆绑程序找不到已安装的 gem

2024-01-28

我正在使用 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,这是因为一篇文章 https://medium.com/@fbzga/how-to-cache-bundle-install-with-docker-7bed453a5800#.5haparbbg展示我们如何保存 gem 下载。因此,当 docker 缓存变热时,它会重新捆绑并永远持续下去。

When I docker build它成功安装了 gem,但无法在捆绑包中找到它们。有人可以帮我持久化宝石,安装我自己的宝石,并让它工作吗?

在我改变之前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

我认为还缺乏GEM_HOME/GEM_PATH在你的代码中。

GEM_HOME/GEM_PATH将被 gem install xxx 用于在特定文件夹中安装 gem。BUNDLE_PATH将被bundle install用来在特定文件夹中安装gems,但不会被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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当 BUNDLE_PATH 使用 Docker 更改时,捆绑程序找不到已安装的 gem 的相关文章

随机推荐