我正在按照说明按照此处的文档所述使用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
我这方面的一些早期调查:
/usr/local/lib/ruby/2.6.0
文件夹中查找宝石,并且看起来确实可疑。我怀疑是版本问题,可能是环境设置不正确。这里是我的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映像ruby:2.5.6时,上面的第二个选项更有可能
[如果您查看Dockerfile的第8-9行,则将在构建Docker映像时看到Gemfile和Gemfile.lock从本地计算机复制到容器中。
我认为这里可能发生的事情是在本地计算机上生成的Gemfile.lock是使用Ruby 2.6.0完成的。
您的本地计算机(不是在容器中)是什么Ruby版本?运行ruby -v
查看。
尝试在本地计算机上安装Ruby 2.6.5并运行bundle install
,以更新Gemfile.lock。
我在移动设备上,目前无法自己测试任何一个。但这是一个起点!