【问题标题】:Docker Compose and Rails: Bundler::GemNotFound upon launching. Is bundler not looking at the right gemset?Docker Compose 和 Rails: Bundler::GemNotFound 在启动时。 bundler 是不是在寻找正确的宝石?
【发布时间】:2020-02-17 17:47:17
【问题描述】:

我正在尝试按照此处文档中所述的说明使用 Docker Compose 设置 Rails 和 Postgres 应用程序 -> https://docs.docker.com/compose/rails/

但是,当我尝试启动应用程序时,在成功设置数据库后,我在运行 docker-compose up 时遇到以下错误:

docker-compose up
Creating docker-and-cms_db_1 ... done
Creating docker-and-cms_web_1 ... done
Attaching to docker-and-cms_db_1, docker-and-cms_web_1
web_1  | bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
web_1  | Bundler::GemNotFound: Could not find bindex-0.8.1 in any of the sources
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:91:in `block in materialize'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:85:in `map!'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:85:in `materialize'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:170:in `specs'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:237:in `specs_for'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:226:in `requested_specs'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/runtime.rb:108:in `block in definition_method'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/runtime.rb:20:in `setup'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler.rb:107:in `setup'
web_1  |   /usr/local/lib/ruby/2.6.0/bundler/setup.rb:20:in `<top (required)>'
web_1  |   /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
web_1  |   /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
docker-and-cms_web_1 exited with code 1

我的一些早期调查:

  • 我正在使用 ruby​​:2.6.5 映像,错误消息似乎表明 bundler 正在尝试在 /usr/local/lib/ruby/2.6.0 文件夹中查找 gem,这看起来确实很可疑。我怀疑是版本问题,可能是环境设置不正确。

这是我的Dockerfile

# From https://docs.docker.com/compose/rails/
FROM ruby:2.6.5

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

RUN bundle install --path vendor/cache

COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

这是我的entrypoind.sh

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

最后这是我的 docker-compose.yml 文件

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

这是我的 Gemfile 的开头(已编辑,因为我认为只有第一行是相关的):

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

ruby '2.6.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.1'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# (...)

调试信息: - 这是在 docker 容器中运行 bundle env 返回的内容:

root@588227887e16:/myapp# bundle env
## Environment


Bundler       1.17.2
  Platforms   ruby, x86_64-linux
Ruby          2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
  Full Path   /usr/local/bin/ruby
  Config Dir  /usr/local/etc
RubyGems      3.0.3
  Gem Home    /usr/local/bundle
  Gem Path    /root/.gem/ruby/2.6.0:/usr/local/lib/ruby/gems/2.6.0:/usr/local/bundle
  User Path   /root/.gem/ruby/2.6.0
  Bin Dir     /usr/local/bundle/bin
Tools
  Git         2.20.1
  RVM         not installed
  rbenv       not installed
  chruby      not installed


## Bundler Build Metadata


Built At          2018-12-19
Git SHA           3fc4de72b
Released Version  false


## Bundler settings


path
  Set for your local app (/usr/local/bundle/config): "vendor/cache"
app_config
  Set via BUNDLE_APP_CONFIG: "/usr/local/bundle"
silence_root_warning
  Set via BUNDLE_SILENCE_ROOT_WARNING: true

感谢您的帮助!

【问题讨论】:

标签: ruby-on-rails docker docker-compose bundler


【解决方案1】:

正如您所说,在 Docker 容器中,Bundler 正在错误的目录中寻找 gem。在容器启动时,Bundler 安装并找到了 Ruby 2.6.0,或者将某些内容复制到容器中以使 Bundler 相信 Ruby 2.6.0 已安装。

当您使用 Docker 镜像 ruby​​:2.5.6 时,上面的第二个选项更有可能

如果您查看 Dockerfile 的第 8-9 行,您会看到 Gemfile 和 Gemfile.lock 在构建 Docker 映像时从本地计算机复制到容器中。

我认为这里可能发生的情况是本地机器上生成的 Gemfile.lock 是使用 Ruby 2.6.0 完成的。

您的本地机器(不在容器中)Ruby 版本是什么?运行ruby -v查看。

尝试在本地机器上安装 Ruby 2.6.5 并运行 bundle install,以更新 Gemfile.lock。

我在我的手机上,目前无法自己测试任何这些。但这是一个开始的地方!

【讨论】:

  • 感谢您的帮助!我本地机器上的 ruby​​ 版本是 ruby​​ 2.6.3p62 。并不是说我以前在本地机器上使用 rvm 来管理我的 Ruby 版本。由于到目前为止它是一个空项目,我将尝试删除我的 Gemfile.lock。
  • 请注意,Gemfile.lock 似乎是用 ruby​​ 2.6.5 生成的(在此处复制文件末尾)。 RUBY 版本 ruby​​ 2.6.5p114 与 1.17.2 捆绑
  • 我编辑了问题,添加了来自容器的 bundle env 结果。
猜你喜欢
  • 2020-08-24
  • 1970-01-01
  • 2014-02-02
  • 2012-03-13
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2015-03-08
相关资源
最近更新 更多