通过Docker在供应商/缓存中使用供应商的gem

问题描述 投票:0回答:1

这是我的docker文件,可正常工作:

FROM ruby:2.6.6
WORKDIR /home/app/webapp
COPY . /home/app/webapp/
RUN bundle install
# Start the main process.
CMD ['/home/app/webapp/entrypoint.sh']

这很好用,但是每次都捆绑安装!取而代之的是,我在git源本身中直接将所有gems供应商放在/ vendor / cache中。

$ ls vendor/cache
<..snip long list.>
rake-13.0.1.gem
rails-5.2.4.1.gem
rails-controller-testing-1.0.4.gem
rails-dom-testing-2.0.3.gem
rails-html-sanitizer-1.3.0.gem
rails-i18n-5.1.3.gem
rails-ujs-0.1.0.gem
sass-rails-5.0.8.gem
sprockets-rails-3.2.1.gem
<..snip long list.>

根据捆绑程序文档:https://bundler.io/bundle_install.html,它应该按照说的那样工作:

[在安装gem时,Bundler将检查供应商/缓存,然后检查您的系统的宝石。如果未缓存或未安装宝石,Bundler将尝试从您在Gemfile中声明的源安装它。

当我在没有docker的Mac上捆绑时,这确实有效,如下所示:

$ bundle 
Using rake 13.0.1
Using rails 5.2.4.1
Using rails-controller-testing 1.0.4
Using rails-i18n 5.1.3
Using rails-ujs 0.1.0
Using sidekiq-pro 4.0.4
Updating files in vendor/cache
Bundle complete! 144 Gemfile dependencies, 304 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

因此,我修改了dockerfile以利用缓存。所以我只捆绑安装一次并将其保存为一层。然后,对于任何代码更改,都无需再次安装捆绑软件]

FROM ruby:2.6.6
WORKDIR /home/app/webapp
COPY Gemfile Gemfile.lock /vendor/cache/ /home/app/webapp/
RUN bundle install
COPY . /home/app/webapp/
# Start the main process.
CMD ['/home/app/webapp/entrypoint.sh']

现在,当我尝试构建它时,出现以下错误:

$ docker-compose build
redis uses an image, skipping
db uses an image, skipping
Building rails
Step 1/6 : FROM ruby:2.6.6
 ---> 107c48f680c0
Step 2/6 : WORKDIR /home/app/webapp
 ---> Using cache
 ---> f23fc51ac8ba
Step 3/6 : COPY Gemfile Gemfile.lock /vendor/cache/ /home/app/webapp/
 ---> 0d1f451b7ee0
Step 4/6 : RUN bundle install
 ---> Running in 5768798c1bcd
Warning: the running version of Bundler (1.17.2) is older than the version that created the lockfile (1.17.3). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
Fetching gem metadata from https://rubygems.org/.......
Could not find sidekiq-pro-4.0.4 in any of the sources
ERROR: Service 'rails' failed to build: The command '/bin/sh -c bundle install' returned a non-zero code: 7

但是,sidekiq-pro gem已在供应商/缓存中

$ ls vendor/cache | grep sidekiq-pro
sidekiq-pro-4.0.4.gem

因此,这在没有docker的本地mac上确实有效,因为捆绑程序从缓存中提取了sidekiq gem,但不适用于docker。关于我还能尝试什么的任何想法?

ruby-on-rails docker dockerfile bundler
1个回答
0
投票

我设法解决了这个问题。需要编辑Dockerfile以在供应商/缓存中复制gem。这是经过修改的gemfile,可以使用并利用docker缓存

FROM ruby:2.6.6
WORKDIR /home/app/webapp
COPY Gemfile Gemfile.lock /home/app/webapp/
COPY vendor/cache /home/app/webapp/vendor/cache/
RUN bundle install
COPY . /home/app/webapp/
# Start the main process.
CMD ['/home/app/webapp/entrypoint.sh'] 

在此处添加答案,以防其他人遇到相同的错误。谢谢您的光临。

© www.soinside.com 2019 - 2024. All rights reserved.