【问题标题】:Docker Container cannot reach localhost port 4444. Why though?Docker Container 无法访问 localhost 端口 4444。为什么?
【发布时间】:2018-01-17 11:41:39
【问题描述】:

我将一个应用程序容器化,它是自动化 Selenium 测试的测试驱动程序。 Selenium 服务器(也称为 Selenium Hub)在 localhost:4444 下的另一个容器和 Firefox 节点中运行。 但我的应用程序无法访问它:

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)

我通过 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"

我猜我的 docker-compose 有一个错误,但我想不通。请帮忙! 顺便说一句:我在 Windows 7 上运行,并使用带有 Vagrant VM 的 Docker。在我的 Vagrantfile 中,我将端口 4444 和 9005 映射到主机系统。如果我打开本地浏览器并访问 localhost:4444,我可以看到 selenium 网格控制台。为什么它在我的应用容器中不起作用?

【问题讨论】:

    标签: selenium docker networking vagrant communication


    【解决方案1】:

    在您的应用容器中localhost 表示当前容器。因此,您需要改用服务的名称。在你的情况下是hub

    所以连接到hub:4444

    【讨论】:

    • 首先我的容器甚至无法到达 hub:4444,但是当我从单个 docker compose 文件启动所有内容并将集线器链接到我的应用程序容器时,一切正常。谢谢
    【解决方案2】:

    迁移到 docker-compose version2 后,我面临同样的问题。 这是我得到的日志

    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.

    下面是 docker-compose yml 文件的样子:

    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"    

    在我的测试中,我尝试使用以下方式访问集线器:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-18
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多