当 BUNDLE_PATH 随 Docker 更改时,捆绑程序找不到已安装的 gem [英] Installed gems not found by bundler when BUNDLE_PATH changed with Docker
问题描述
我正在使用 docker 开发 Rails 应用程序.docker 文件如下所示:
FROM ruby:1.9.3运行 apt-get update -qq &&apt-get install -y build-essential libpq-dev vimENV APP_HOME/next-reg运行 mkdir $APP_HOME工作目录 $APP_HOMEENV BUNDLE_PATH/box添加 .$APP_HOME运行 gem install gem1.gem gem2.gem复制 Gemfile Gemfile复制 Gemfile.lock Gemfile.lock运行捆绑安装
如您所见,我更改了 BUNDLE_PATH
,这是因为 一篇文章展示了我们如何保持 gem 下载.因此,当 docker 缓存变热时,它会重新捆绑并占用 FOREVER.
当我 docker build
成功安装 gems 时,它无法在捆绑包中找到它们.有人可以帮我处理持久化 gem、安装我自己的 gem 并让它工作吗?
在我更改 BUNDLE_PATH
之前,构建工作正常,它只是经常重新捆绑而不更改 gem 文件(因为,我猜 docker 图像缓存变热了).
我的 docker-compose 是这样的:
数据库:图片:postgres卷:- ~/.docker-voumes/postgres/data:/var/lib/postgresql/data# 这是为了保存和持久化 ruby gems,在 web 和 web 的 dockerfile 中引用.宝石文件:图片:忙箱卷:-/盒子网络:建造: .命令:bundle exec rails s -p 3000 -b '0.0.0.0'卷:- .:/下一个注册卷_来自:- gem_files端口:- 3000:3000"- 8000:8000"链接:- D b环境文件:- .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运行 apt-get update -qq &&apt-get install -y build-essential libpq-dev vimENV APP_HOME/next-reg运行 mkdir $APP_HOME工作目录 $APP_HOMEENV BUNDLE_PATH/boxENV GEM_PATH/boxENV GEM_HOME/box添加 .$APP_HOME运行 gem 安装捆绑程序运行 gem install tzinfo -v 1.2.2复制 Gemfile Gemfile运行捆绑安装
有了这个 Gemfile:
来源 'https://rubygems.org'宝石'tzinfo','1.2.2'
Wich 将产生:
<上一页>步骤 11/13:运行 gem install tzinfo -v 1.2.2---> 运行在 8a87fa54fa19成功安装thread_safe-0.3.6成功安装tzinfo-1.2.2已安装 2 个宝石---> 3c91d59bde8a卸下中间容器 8a87fa54fa19步骤 13/13:运行捆绑安装---> 在 20f1e4ec93b1 中运行不要以 root 身份运行 Bundler.如果需要,Bundler 可以要求 sudo,并且以 root 身份安装您的捆绑软件将破坏所有非 root 用户的此应用程序这台机器上的用户.从 https://rubygems.org/获取 gem 元数据...从 https://rubygems.org/获取版本元数据.解决依赖关系...Rubygems 1.8.23.2 不是线程安全的,因此您的 gem 将一次安装一个.升级到 Rubygems 2.1.0 或更高版本以启用并行 gem 安装.安装 rake 12.0.0使用 thread_safe 0.3.6使用捆绑器 1.14.6使用 tzinfo 1.2.2捆绑完成!2 个 Gemfile 依赖项,现在安装了 4 个 gem.捆绑的 gem 安装在/box 中.正如您在结果输出中看到的那样,bundle install
重新使用了 gem install
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
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
这篇关于当 BUNDLE_PATH 随 Docker 更改时,捆绑程序找不到已安装的 gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!