通过Docker在供应商/缓存中使用已售出的宝石 [英] Using vendored gems in vendor/cache with Docker

查看:13
本文介绍了通过Docker在供应商/缓存中使用已售出的宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的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']

这工作得很好,但它每次都会捆绑安装!取而代之的是,我将所有的gem供应商都直接放在了git源代码本身的/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将检查供应商/缓存,然后检查您的 系统的精华。如果没有缓存或安装gem,bundler将尝试 从您在Gemfile中声明的源安装它。

当我在没有坞站的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上是有效的,因为bundler从缓存中拾取idekiq gem,但它不能与docker一起工作。你知道我还能尝试什么吗?

推荐答案

我设法解决了这个问题。需要编辑Docker文件以在供应商/缓存中复制宝石。以下是修改后的gefile文件,该文件有效并利用了扩展坞缓存

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'] 
在此处添加答案,以防其他人遇到相同的错误。感谢您的关注。

这篇关于通过Docker在供应商/缓存中使用已售出的宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆