Rails找不到rake gem。

19

我有一个基本的Rails应用程序在Docker中运行。 我可以很好地启动它并提供响应。 我可以无问题地运行rake -T。 但是,我无法运行Rails generate命令。 它失败并告诉我Could not find rake-12.3.1 in any of the sources

$ docker-compose run main bundle exec rails generate controller Welcome index
Starting cognitopoc_db_1 ... done
Could not find rake-12.3.1 in any of the sources
Run `bundle install` to install missing gems.

这是我的捆绑环境:

$ docker-compose run main bundle env 
Starting cognitopoc_db_1 ... done
## Environment

Bundler       1.16.1
  Platforms   ruby, x86_64-linux
Ruby          2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  Full Path   /usr/local/bin/ruby
  Config Dir  /usr/local/etc
RubyGems      2.7.6
  Gem Home    /usr/local/bundle
  Gem Path    /root/.gem/ruby/2.5.0:/usr/local/lib/ruby/gems/2.5.0:/usr/local/bundle
  User Path   /root/.gem/ruby/2.5.0
  Bin Dir     /usr/local/bundle/bin
Tools         
  Git         2.11.0
  RVM         not installed
  rbenv       not installed
  chruby      not installed

## Bundler Build Metadata

Built At          2017-12-21
Git SHA           0034ef341
Released Version  true

## Bundler settings

path
  Set for your local app (/usr/local/bundle/config): "vendor/cache"
  Set via BUNDLE_PATH: "/usr/local/bundle"
jobs
  Set for your local app (/usr/local/bundle/config): "4"
bin
  Set via BUNDLE_BIN: "/usr/local/bundle/bin"
silence_root_warning
  Set via BUNDLE_SILENCE_ROOT_WARNING: true
app_config
  Set via BUNDLE_APP_CONFIG: "/usr/local/bundle"
gemfile
  Set via BUNDLE_GEMFILE: "/myapp/Gemfile"

## Gemfile

### Gemfile

ruby
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
  "https://github.com/#{repo_name}.git"
end

gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.7'
gem 'rails', '~> 5.1.6'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'uglifier', '>= 1.3.0'

# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

group :development, :test do
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  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

### Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.1.6)
      actionpack (= 5.1.6)
      nio4r (~> 2.0)
      websocket-driver (~> 0.6.1)
    actionmailer (5.1.6)
      actionpack (= 5.1.6)
      actionview (= 5.1.6)
      activejob (= 5.1.6)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.1.6)
      actionview (= 5.1.6)
      activesupport (= 5.1.6)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.1.6)
      activesupport (= 5.1.6)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    activejob (5.1.6)
      activesupport (= 5.1.6)
      globalid (>= 0.3.6)
    activemodel (5.1.6)
      activesupport (= 5.1.6)
    activerecord (5.1.6)
      activemodel (= 5.1.6)
      activesupport (= 5.1.6)
      arel (~> 8.0)
    activesupport (5.1.6)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    addressable (2.5.2)
      public_suffix (>= 2.0.2, < 4.0)
    arel (8.0.0)
    builder (3.2.3)
    byebug (10.0.2)
    capybara (2.18.0)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (>= 2.0, < 4.0)
    childprocess (0.9.0)
      ffi (~> 1.0, >= 1.0.11)
    coffee-rails (4.2.2)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.12.2)
    concurrent-ruby (1.0.5)
    crass (1.0.3)
    erubi (1.7.1)
    execjs (2.7.0)
    ffi (1.9.23)
    globalid (0.4.1)
      activesupport (>= 4.2.0)
    i18n (1.0.0)
      concurrent-ruby (~> 1.0)
    jbuilder (2.7.0)
      activesupport (>= 4.2.0)
      multi_json (>= 1.2)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.2.2)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.0)
      mini_mime (>= 0.1.1)
    method_source (0.9.0)
    mini_mime (1.0.0)
    mini_portile2 (2.3.0)
    minitest (5.11.3)
    multi_json (1.13.1)
    nio4r (2.3.0)
    nokogiri (1.8.2)
      mini_portile2 (~> 2.3.0)
    pg (1.0.0)
    public_suffix (3.0.2)
    puma (3.11.3)
    rack (2.0.4)
    rack-test (1.0.0)
      rack (>= 1.0, < 3)
    rails (5.1.6)
      actioncable (= 5.1.6)
      actionmailer (= 5.1.6)
      actionpack (= 5.1.6)
      actionview (= 5.1.6)
      activejob (= 5.1.6)
      activemodel (= 5.1.6)
      activerecord (= 5.1.6)
      activesupport (= 5.1.6)
      bundler (>= 1.3.0)
      railties (= 5.1.6)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.0.4)
      loofah (~> 2.2, >= 2.2.2)
    railties (5.1.6)
      actionpack (= 5.1.6)
      activesupport (= 5.1.6)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (12.3.1)
    rb-fsevent (0.10.3)
    rb-inotify (0.9.10)
      ffi (>= 0.5.0, < 2)
    ruby_dep (1.5.0)
    rubyzip (1.2.1)
    sass (3.5.6)
      sass-listen (~> 4.0.0)
    sass-listen (4.0.0)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
    sass-rails (5.0.7)
      railties (>= 4.0.0, < 6)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    selenium-webdriver (3.11.0)
      childprocess (~> 0.5)
      rubyzip (~> 1.2)
    spring (2.0.2)
      activesupport (>= 4.2)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (3.7.1)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    thor (0.20.0)
    thread_safe (0.3.6)
    tilt (2.0.8)
    turbolinks (5.1.0)
      turbolinks-source (~> 5.1)
    turbolinks-source (5.1.0)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    uglifier (4.1.8)
      execjs (>= 0.3.0, < 3)
    websocket-driver (0.6.5)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.3)
    xpath (3.0.0)
      nokogiri (~> 1.8)

PLATFORMS
  ruby

DEPENDENCIES
  byebug
  capybara (~> 2.13)
  coffee-rails (~> 4.2)
  jbuilder (~> 2.5)
  listen (>= 3.0.5, < 3.2)
  pg (>= 0.18, < 2.0)
  puma (~> 3.7)
  rails (~> 5.1.6)
  sass-rails (~> 5.0)
  selenium-webdriver
  spring
  spring-watcher-listen (~> 2.0.0)
  turbolinks (~> 5)
  uglifier (>= 1.3.0)

BUNDLED WITH
   1.16.1

这是我的Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
  "https://github.com/#{repo_name}.git"
end

gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.7'
gem 'rails', '~> 5.1.6'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'uglifier', '>= 1.3.0'

group :development, :test do
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

这是Dockerfile文件的内容:
FROM ruby:2.5

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -qq \
  && apt-get install -y \
    apt-utils \
    build-essential \
    nodejs

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile* /myapp/
RUN bundle install --path vendor/cache  --jobs=4

COPY . /myapp

CMD bundle exec rails s -p 3000 -b '0.0.0.0'

这里是Compose文件:

version: '3'

services:

  db:
    image: postgres
    volumes:
      - ./pg_data:/var/lib/postgresql/data
  main:
    build: .
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

那么为什么我能运行rake -T,但不能运行rails generate呢?

2
今天我也遇到了完全相同的问题(rake 12.3.1 - docker compose)。 - opsb
bundle show rake 报告了什么? - AndrewK
5个回答

8

当我将Bitbucket管道容器升级为Ruby 2.5.1容器时,我开始出现该错误。尽管它可能不能解决每个人的用例,但我通过将以下内容添加到我的bitbucket-pipelines.yml文件中,成功使我的rails(rake)命令正常工作:

bundle install --binstubs

如果您看到此消息,则表示您的./bin/rails binstub是由Bundler而不是Rails生成的。我在运行上述命令后遇到了该错误。当我运行docker-compose up时,就会出现这个错误。 - Steven Aguilar
1
问题在于当我使用 bundle install --path vendor/bundle 命令后,尝试执行 bundle exec rails ... 时,它无法找到安装在 vendor/bundle 中的 gem 包。 - AndrewK

2
有趣的是,在使用docker-compose进行捆绑时,一些人在同一时间遇到了相同版本的Rake错误。 我无法确定原因,但我找到了一个对我有效的解决方案。
您可以尝试执行以下命令来删除您的Gemfile.lock和.bundle目录,并重新捆绑。 我从这篇博客文章中得到了灵感:https://makandracards.com/makandra/1667-how-to-fix-gems-are-unavailable-although-they-are-installed
$ docker-compose exec main bash
$ rm -rf .bundle/
$ rm Gemfile.lock
$ bundle install
$ rails c # => This now works

1

在docker-compose.yml中定义BUNDLE_APP_CONFIG可能会解决这个问题。

environment:
  BUNDLE_APP_CONFIG: /myapp/.bundle

在执行完上述步骤后,再次运行bundle install

1
如果您不想禁用Spring,请不要使用任何建议的解决方案:
  1. DISABLE_SPRING变量,因为它会禁用Spring
  2. bundle install --binstubs,因为它会覆盖您的bin/rails和其他可执行文件以避免使用Spring。
我正在使用Rails 5.1.6和Ruby 2.6.6,Bundler 2.1.4,Spring 2.0.0。阅读了一些spring gem的PR和Changelog后,我将spring更新到了2.1.1,然后问题得到了解决。所以我仍然可以在docker容器中使用spring。

0
我通过禁用Spring来解决这个问题:DISABLE_SPRING=1 bin/rails console

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接