Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to reach localhost from inside Selenium docker

I'm trying to run my tests using Selenium docker, I have a local grunt server running on port 9000, I' launched the following selenium docker:

docker run  -d -p 4444:4444 -p 5900:5900 selenium/standalone-chrome-debug

Then I've launched my tests (using Capybara) and opened VNC to watch the tests, but all I get is chrome messgae "This site can’t be reached".

cabybara.rb:

isWindows = (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil

require 'capybara/rspec'
require 'capybara'
require 'capybara/dsl'
require_relative 'sinatra_proxy'
require 'selenium/webdriver'
require 'selenium/webdriver/remote/http/curb' if !isWindows

Capybara.register_driver :selenium_chrome do |app|
    http_client = isWindows ? nil : Selenium::WebDriver::Remote::Http::Curb.new
    options = {
        http_client: http_client,
        browser: :chrome,
        # service_log_path: 'chromedriver.out', # Enable Selenium logs
        switches: ["--disable-web-security", '--user-agent="Chrome under Selenium for Capybara"']
    }
    options[:url] = "http://172.17.0.2:4444/wd/hub"
    Capybara::Selenium::Driver.new app, options

end

Capybara.default_driver = :selenium_chrome
Capybara.app = SinatraProxy.new
Capybara.app_host = "http://127.0.0.1:9000"
Capybara.server_host = '0.0.0.0'

ip addr show docker0

ip addr show docker0                                                                  
6: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:22:ec:65:9e:f1 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe40::42:ecdd:fe73:9ef4/64 scope link 
       valid_lft forever preferred_lft forever

Needed to be the IP of docker host, used:

ip route show | grep docker0 | awk '{print $9}' 

for Capybara.app_host (DOCKER_HOST_IP:PORT) and Capybara.server_host (DOCKER_HOST_IP)

like image 488
Captain_Meow_Meow Avatar asked Oct 23 '16 12:10

Captain_Meow_Meow


People also ask

Can I access localhost from docker?

Docker provides a host network which lets containers share your host's networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself. Now your container can reference localhost or 127.0. 0.1 directly.

How do I make my docker talk to localhost?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.

How do I connect to a docker container locally?

To connect to a container using plain docker commands, you can use docker exec and docker attach . docker exec is a lot more popular because you can run a new command that allows you to spawn a new shell. You can check processes, files and operate like in your local environment.


1 Answers

Use:

Capybara.app_host = "http://yourhostip:9000" 

not localhost. For docker container localhost is itself.

Also I recommend not calling docker by its internal ip just use:

options[:url] = "http://localhost:4444/wd/hub"

But first solve the former problem

Regards

like image 129
Carlos Rafael Ramirez Avatar answered Nov 12 '22 17:11

Carlos Rafael Ramirez