RSpec.configure do |config|
...
  config.before(:each) do
    if ENV['SELENIUM_PORT']
      server = Capybara.current_session.server
      Capybara.app_host = server ?
        "http://#{server.host}:#{server.port}" :
        "http://localhost:9887"
    end
  end

   config.after(:each) do
    Capybara.reset_sessions!
    Capybara.use_default_driver
    Capybara.app_host = nil
  end
...
Capybara.configure do |config|
...
  if ENV['SELENIUM_PORT']
    if RUBY_PLATFORM.match(/linux/)
      config.server_host = `/sbin/ip route|awk '/scope/ { print $9 }'`.chomp
    else
      config.server_host = '127.0.0.1'
    end
  end
end

Capybara.register_driver :chrome do |app|
  if ENV['SELENIUM_PORT']
    uri = URI(ENV['SELENIUM_PORT'])
    Capybara::Selenium::Driver.new(app, browser: :remote, 
      url: "http://#{uri.host}:#{uri.port}/wd/hub", desired_capabilities: :chrome)
  else
    Capybara::Selenium::Driver.new(app, browser: :chrome)
  end
end
Capybara.javascript_driver = :chrome