【发布时间】: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
感谢您的帮助!
【问题讨论】:
-
很久以前就有odl Rails 容器运行,这是环境问题。看看这个(很老)github.com/noxsnono/Docker/blob/master/ruby232_sql_rails/…
标签: ruby-on-rails docker docker-compose bundler