ruby-on-rails - docker-compose Rails spring 不起作用

标签 ruby-on-rails docker docker-compose rails-spring

我用

  • Rail 4.2.5.1 在容器中运行
  • Ruby 2.3.1 在容器中运行

当我开始

 $ docker-compose exec web /bin/bash # ssh into the web server
 $ bundle exec rails c 

我收到以下错误。

from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/runtime.rb:86:in `each'
    from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/runtime.rb:86:in `block in require'
    from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/runtime.rb:75:in `each'
    from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/runtime.rb:75:in `require'
    from /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler.rb:106:in `require'
    from /usr/src/app/config/application.rb:7:in `<top (required)>'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:82:in `require'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:82:in `preload'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:143:in `serve'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
    from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

但是我已经将 pq gem 放入 Gemfile 中。 我已经尝试过以下操作,

$ bundle update pg
$ bundle uninstall pg && bundle install
$ bundle exec spring binstub --all

但是我没有运气。

当我尝试时,

$ docker-compose exec web /bin/bash # ssh into the web server
$ DISABLE_SPRING=1 bundle exec rails c

它确实有效。所以我认为 spring gem 不能正常工作。 另外,如果我在本地做同样的事情,它确实有效。

我认为我的 docker_compose.yml 设置不正确。 有什么想法吗?

docker-compose.yml

  web:
    depends_on:
      - 'postgres'
      - 'redis'
    build: .
    command: rails server -b 0.0.0.0
    ports:
      - '3000:3000'
    volumes:
      - '.:/usr/src/app'
    env_file:
      - '.env'

  postgres:
    image: 'postgres:9.4.4'
    environment:
      POSTGRES_USER: 'postgres'
    ports:
      - '5432:5432'

  redis:
    image: 'redis:3.2-alpine'
    command: redis-server
    ports:
      - '6379:6379'

  worker:
    depends_on:
      - 'postgres'
      - 'redis'
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml.erb 
    volumes:
      - '.:/usr/src/app'
    env_file:
      - '.env'

最佳答案

通过bin/spring调用容器内的应用程序:

$ docker-compose exec web bash
$ bin/spring rails c

运行测试等也是如此:

$ bin/spring rspec

单独运行 spring 以获得一些额外的提示:

$ bin/spring

我使用 Ruby 2.4.3、Rails 5.1.4 和 Spring 2.0.2 对此进行了测试。

关于ruby-on-rails - docker-compose Rails spring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012630/

相关文章:

ruby-on-rails - 如何检查日期是否有效?

ruby-on-rails - Rails ActiveRecord : HABTM find parameters

docker - 为什么可以在 Docker 镜像中手动运行 Java 程序,但不能作为入口点

heroku - 自定义buildpack中的权限被拒绝

ruby-on-rails - 在 docker 下应该将 actioncable URL 设置为什么?

ruby-on-rails - 事件记录错误消息form_tag Rails 3

docker 基础图像: how to upgrade

docker - 如何配置 mariadb docker-compose 文件以使用 3306 以外的其他端口?

ruby-on-rails - 通过 docker-compose 使用远程 byebug 实例

ruby-on-rails - 如何在 Rails 中添加禁用的提交按钮