找不到 rails 宝石

6
我正在尝试构建一个Docker容器,但是遇到以下错误信息:
Step 8/12 : RUN bundle binstubs bundler --force
 ---> Running in ed94b127974b
Could not find gem 'rails (>= 5.1.5, ~> 5.1)' in any of the gem sources listed
in your Gemfile.
ERROR: Service 'dockerzon' failed to build: The command '/bin/sh -c bundle binstubs bundler --force' returned a non-zero code: 7

我已经尝试在我的Gemfile中使用不同版本的Rails,但没有成功!这是我在我的Gemfile中所写的内容:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
# gem 'rails', '4.2.6'
# gem 'rails', '>= 5.1.5'
gem 'rails', '~> 5.1', '>= 5.1.5'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.7'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 4.1.6'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 1.0.0', group: :doc

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

# Use Unicorn as the app server
# gem 'unicorn'

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

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

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 3.5.1'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

gem 'pg', '~> 1.0.0'
gem 'redis-rails', '~> 5.0.2'
gem 'sidekiq', '~> 5.1.1'
gem 'puma', '~> 3.11.2'
gem 'rack-timeout', '~> 0.4.2'

这是我的Dockerfile:

# Use the barebones version of Ruby 2.5
FROM ruby:2.5-slim

# Optionally set a maintainer name to let people know who made this image.

# Install dependencies:
# - build-essential: To ensure certain gems can be compiled
# - nodejs: Compile assets
# - libpq-dev: Communicate with postgres through the postgres gem
RUN apt-get update && apt-get install -qq -y --no-install-recommends \
      build-essential nodejs libpq-dev

# Set an environment variable to store where the app is installed to inside
# of the Docker image. The name matches the project name out of convention only.
ENV INSTALL_PATH /dockerzon
RUN mkdir -p $INSTALL_PATH

# This sets the context of where commands will be ran in and is documented
# on Docker's website extensively.
WORKDIR $INSTALL_PATH

# Ensure gems are cached and only get updated when they change. This will
# drastically increase build times when your gems do not change.
COPY Gemfile Gemfile

# We want binstubs to be available so we can directly call sidekiq and
# potentially other binaries as command overrides without depending on
# bundle exec.
# RUN bundle install --binstubs
RUN bundle binstubs bundler --force

# Copy in the application code from your work station at the current directory
# over to the working directory.
COPY . .

# Provide a dummy DATABASE_URL to Rails so it can pre-compile assets.
RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname SECRET_TOKEN=dummytoken assets:precompile

# Ensure the static assets are exposed through a volume so that nginx can read
# in these values later.
VOLUME ["$INSTALL_PATH/public"]

# The default command that gets ran will be to start the Puma server.
CMD bundle exec puma -C config/puma.rb

我可以问一下,为什么要使用gem 'rails', '~> 5.1', '> = 5.1.5'而不是gem 'rails', '~> 5.1.5' - mdesantis
2个回答

7

尝试更新您的RubyGems版本:

gem update --system

然后在Gemfile中将这一行改为:

gem 'rails', '~> 5.1.5'

现在运行bundler安装宝石(gems):
bundle install

1
谢谢@Kandy。如果我像你建议的那样运行"bundle install",这个方法可以正常工作,但是如果我尝试使用"bundle binstubs bundler --force",它就不起作用了。 - gbaz
仍然在2021年有用。谢谢 :) - Gtx
错误:您的RubyGems是通过APT安装的,通过RubyGems自身升级它是不受支持的。如果您确实需要最新版本的RubyGems - saber tabatabaee yazdi

1
在你的Dockerfile中添加以下行:

RUN gem install rails

ruby:2.5-slim这个Docker镜像不包含rails,只包含ruby。你无法使用Gemfile安装rails。在捆绑gemfile之前,您必须先安装rails。

谢谢你的回复,@Atish。现在我在文件中的下一个gem出现了错误! 在您的Gemfile中列出的任何gem源中都找不到gem 'sass-rails(〜> 5.0.7)'。 错误:服务“dockerzon”构建失败:命令'/ bin / sh -c bundle binstubs bundler --force'返回非零代码:7 - gbaz
Gemfile 中删除 saas-rails 的 '> 5.0.7' 和 coffee-rails 的 '> 4.2.2'。让 gem 文件使用这些 gem 的已安装版本。必须安装 rails 5.1 版本,这是最新版本,因此 saas-rails 5.0 版本会出现未找到错误。 - Atish
第9/13步骤失败了:运行命令RUN bundle binstubs bundler --force ---> 正在运行 bd13ecd38b04 容器 在Gemfile中列出的任何gem源中都找不到gem 'sass-rails'。 错误:服务'dockerzon'构建失败:命令'/bin/sh -c bundle binstubs bundler --force'返回非零代码:7。如果我运行"bundle install",则从Gemfile安装gems,但如果我运行bundle binstubs bundler --force,则无法安装。 - gbaz

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