【问题标题】:Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sourcesBundler::GemNotFound:在任何源中都找不到 rake-12.3.2
【发布时间】:2019-11-16 14:19:46
【问题描述】:

运行带有卷的 rails docker 容器时出错 捆绑器:加载命令失败:rails (/usr/local/bundle/bin/rails) Bundler::GemNotFound:在任何源中都找不到 rake-12.3.2

我可以在没有音量的情况下运行我的 rails docker 容器。 但是当我这样附加音量时:

docker run --name rails-chat-tutorial-web \
            -e DATABASE_HOST=172.17.0.1 \
            -e DATABASE_PORT=5432 \
            -e DATABASE_USERNAME=postgres \
            -e DATABASE_PASSWORD=postgres \
            -e REDIS_URL=redis://172.17.0.1:6379/1 \
            -p 3000:3000 \
            -v $(pwd):/application rails-chat-tutorial

我会得到这个错误输出:

bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:87:in `block in materialize'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:81:in `map!'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:81:in `materialize'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:170:in `specs'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:237:in `specs_for'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:226:in `requested_specs'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/runtime.rb:108:in `block in definition_method'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/runtime.rb:20:in `setup'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler.rb:107:in `setup'
  /usr/local/bundle/gems/bundler-2.0.1/lib/bundler/setup.rb:20:in `<top (required)>'
  /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
  /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'

我尝试在我的 Dockerfile 中包含这些行,但仍然收到错误:

RUN gem install rake -v '12.3.2'
RUN bundle install --binstubs
RUN bundle install --path vendor/bundle
RUN bundle install --local
RUN bun

dle install --local --path=vendor/cache

这是我的 Dockerfile:

FROM ruby:2.5.0-stretch

COPY ./Gemfile ./application/
COPY ./Gemfile.lock ./application/

WORKDIR /application

ENV BUNDLER_VERSION 2.0.1

RUN gem install bundler -v '2.0.1' 
RUN bundle install --deployment --without development test 
RUN apt-get update -qq && apt-get install -y build-essential 
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - 
RUN apt-get install -y nodejs 
RUN bundle install --local --path=vendor/cache

RUN npm install yarn -g

COPY . .

ENV RAILS_ENV production 
ENV SECRET_KEY_BASE production_test_key rails c

RUN bundle exec rake assets:precompile

EXPOSE 3000

CMD bundle exec rails server

Gemfile 的内容:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

group :production do
  gem 'pg'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'devise'
gem 'bootstrap', '~> 4.3.1'
gem 'jquery-rails'
gem 'simple_form'
gem 'redis'
gem 'httparty', '~> 0.17.0'
gem 'rake', '12.3.2'

如果我使用 shell 和 gem 列表运行 rails 容器,我会得到 'rake (12.3.2, 12.3.0)'

过去 2 天我一直在做这个,但没有任何进展。

提前感谢那些能够提供一些指导的人。

【问题讨论】:

  • Gemfile的内容是什么?
  • 我已经添加了我的 Gemfile @atline
  • 尝试在 Gemfile 中删除 ruby '2.5.0' 并重新构建映像。
  • 试过了。我仍然得到相同的错误输出。 @atline

标签: docker ruby-on-rails-5 docker-volume


【解决方案1】:

当您添加docker run -v $(pwd):/application 选项时,它会隐藏图像中/application 目录中的所有内容,并将其替换为您主机系统中的内容。这尤其包括/application/vendor 目录:Dockerfile 中的任何bundle 命令都将被完全忽略,而是使用主机系统的./vendor 目录。

如果您必须在已部署的容器中进行实时编辑和重新加载,则没有一个很好的答案。 Node 生态系统是类似的(第三方库在 ./node_modules),大多数类似的问题是关于 Node 而不是 Ruby。 Add a volume to Docker, but exclude a sub-folder 建议为./vendor 添加匿名卷; 仅在您第一次运行应用程序时,它将从图像中填充,但if you later change your Gemfile it won't get updatedreplicating this setup is unnecessarily complicated in cluster environments like Kubernetes

如果您想尝试匿名卷路径,可能看起来像

docker run --name rails-chat-tutorial-web ... \
  -v $PWD:/application -v /application/vendor \
  rails-chat-tutorial

$EDITOR Gemfile
bundle install
docker stop rails-chat-tutorial-web
docker rm -v rails-chat-tutorial-web
docker run ...

docker rm -v 将删除匿名卷,它会在下一个docker run 重新创建并更新内容。你告诉 Docker 该目录包含必须在运行期间保留的重要非代码数据。)

对我来说效果很好的顺序是在完全不了解 Docker 的情况下开发我的应用程序:在本地开发它,运行它,编写好的 rspec 测试,并且普遍认为它可以工作。只有这样我才docker builddocker run 一个图像,而不将我的源代码绑定到其中。如果它发生故障,我会在本地开发环境中重现该问题,为其编写测试并修复它,然后重复 docker build; docker run 序列。

【讨论】:

  • 非常感谢您的解释。我一定会尝试你的建议。我对 docker 很陌生,并试图制作一个多 docker 应用程序。这就是我尝试设置多 docker 环境并首先对其进行测试的原因。
  • 请问'$EDITOR Gemfile' 是什么意思?是命令吗?
  • 无论您使用什么命令来编辑源文件,例如您的应用程序的Gemfile
猜你喜欢
  • 2018-04-08
  • 2012-04-19
  • 2015-03-04
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多