gpt4 book ai didi

ruby-on-rails - 当 BUNDLE_PATH 随 Docker 更改时,捆绑程序找不到已安装的 gem

转载 作者:IT老高 更新时间:2023-10-28 21:26:19 25 4
gpt4 key购买 nike

我正在使用 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展示我们如何保持 gem 下载。因此,当 docker 缓存变热时,它会重新捆绑并占用 FOREVER。

当我 docker build 成功安装 gems 时,它无法在 bundle 中找到它们。有人可以帮我处理持久化 gem、安装我自己的 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'

Wich 将产生:

Step 11/13 : RUN gem install tzinfo -v 1.2.2 ---> Running in 8a87fa54fa19Successfully installed thread_safe-0.3.6Successfully installed tzinfo-1.2.22 gems installed ---> 3c91d59bde8aRemoving intermediate container 8a87fa54fa19Step 13/13 : RUN bundle install ---> Running in 20f1e4ec93b1Don't run Bundler as root. Bundler can ask for sudo if it is needed, andinstalling your bundle as root will break this application for all non-rootusers 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.0Using thread_safe 0.3.6Using bundler 1.14.6Using tzinfo 1.2.2Bundle complete! 2 Gemfile dependencies, 4 gems now installed.Bundled gems are installed into /box.

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

中的预加载 gem

关于ruby-on-rails - 当 BUNDLE_PATH 随 Docker 更改时,捆绑程序找不到已安装的 gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35020095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com