Docker Container cannot reach localhost port 4444.

2019-08-18 11:53发布

问题:

I containerized an application which is the test driver of an automated Selenium test. The Selenium server (also called Selenium Hub) is running in another Container, as well as the Firefox Node, under localhost:4444. But my app is not able to reach it:

Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: '10d3b5fd1010', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_11            1'
Driver info: driver.version: RemoteWebDriver
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:665)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:158)
        at de.services.impl.TestSetupFactory.getWebDriver(TestSetupFactory.java:408)
        at de.services.impl.TestSetupFactory.getSeleniumService(TestSetupFactory.java:279)
        at de.services.impl.AutomationServiceImpl.executeTests(AutomationServiceImpl.java:220)
        at de.start.Start.main(Start.java:25)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:4444 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: C            onnection refused (Connection refused)
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)

I started these containers via docker-compose:

version: '2'
services:
  hub:
    image: selgrid:1.1
    ports:
      - "4444:4444"

  firefox:
    #pull latest from docker hub
    image: selenium/node-firefox
    volumes:
      - /dev/urandom:/dev/random
    depends_on:
      - hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=hub
      - HUB_PORT_4444_TCP_PORT=4444
  testautomation:
    #run testautomation app
    image: volumetest
    links:
      - "hub"
    ports:
      - "9005:9005"

I guess there is a mistake in my docker-compose, but I can't figure it out. Please help! btw: I am running on Windows 7 and using Docker with a Vagrant VM. In my Vagrantfile I mapped port 4444 and 9005 to the host system. If I open my local browser and reach for localhost:4444, I can see the selenium grid console. Why it won't work from my app container?

回答1:

In your app container localhost means the current container. So you need to use the name of the service instead. Which in your case is hub

So connect to hub:4444



回答2:

I am facing the same issue after moving to docker-compose version2. This the log that I get

Connect to localhost:4444 [localhost/127.0.0.1] failed: Connection refused

org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

Below is how docker-compose yml file looks like:

version: '2'
services:
  seleniumhub:
    image: selenium/hub
    ports:
      - 4444:4444
  firefoxnode:
    image: selenium/node-firefox-debug
    ports:
      - 5900
    environment:
        - HUB_PORT_4444_TCP_ADDR=seleniumhub
        - HUB_PORT_4444_TCP_PORT=4444
  webdrivertests:
    image: vodqa/gridtests
    volumes:
      - ./temp:/usr/src/app/target
    environment:
        - HUB_PORT_4444_TCP_ADDR=seleniumhub
        - HUB_PORT_4444_TCP_PORT=4444
    command: bash -c "cd /usr/src/app && mvn test"    

Inside my test, I try to access the hub using:

new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),desiredCapabilitiesff   );