如何通过 docker-compose 而不是使用 docker 来启动 selenium hub 和一个链接节点?

How to start selenium hub and one linked node via docker-compose instead of using docker?

我可以通过以下方式启动 selenium hub 映像:

docker run --rm=true -P -p 4444:4444  --name selenium-hub selenium/hub

并通过以下方式添加 firefox worker:

docker run --rm=true --link selenium-hub:hub selenium/node-firefox

继续http://localhost:4444/grid/console然后会很好地显示网格。

我不想每次都使用 docker,但通过 docker-compose 进行相同的设置。

因此,我想我可以在 docker-compose.yml:

中做到这一点
selenium_hub:
    image: selenium/hub
    ports: ["4444:4444"]
    links:
        - selenium_firefox_worker
selenium_firefox_worker:
    image: selenium/node-firefox

但在 运行 docker-compose up 之后我收到消息:

selenium_firefox_node_1 | Not linked with a running Hub container
selenium_firefox_node_1 exited with code 1

因此网格不显示任何节点。

我认为我可能以错误的顺序进行链接,但甚至:

selenium_hub:
    image: selenium/hub
    ports: ["4444:4444"]
selenium_firefox_node:
    image: selenium/node-firefox
    links:
        - selenium_hub

产生同样的错误。

我做错了什么?

Stumbling across this tutorial,提供了这个语法。尽管它与我的一种方法类似,但它确实有效。

hub:
  image: selenium/hub
  ports:
    - "4444:4444"
firefox:
  image: selenium/node-firefox
  links:
    - hub
chrome:
  image: selenium/node-chrome
  links:
    - hub

好像和命名有关,但我不确定。

selenium_hub:
    image: selenium/hub
    ports: ["4444:4444"]
selenium_firefox_node:
    image: selenium/node-firefox
    links:
        - "selenium_hub:hub"

虽然 确实有效,但我只是想详细说明它失败的原因。

节点容器希望连接到一个可以简单解析的集线器:

hub

而不是在他们的例子中,它将被解析为:

selenium_hub

附带说明一下,如果使用 docker-compose 版本 2 格式,您必须指定几个环境变量,否则节点将无法连接到集线器:

version: '2'
services:
    hub:
        image: selenium/hub
        ports:
            - "4444:4444"

    firefox:
        image: selenium/node-firefox
        environment:
            HUB_PORT_4444_TCP_ADDR: hub
            HUB_PORT_4444_TCP_PORT: 4444
        links:
            - hub

学分: