How to start selenium hub and one linked node via

2019-04-29 11:52发布

问题:

I can start a selenium hub image via:

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

and add a firefox worker via:

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

Going on http://localhost:4444/grid/console then will show the grid just fine.

I don't want to use docker each time but have the same setup via docker-compose.

Hence, I thought I could just do this in my docker-compose.yml:

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

Yet after running docker-compose up I get the message:

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

and hence the grid doesn't show any node.

I thought that I may be doing the linking in the wrong order, yet even:

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

yields in the same error.

What am I doing wrong?

回答1:

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

While k0pernikus' answer does work, I just wanted to elaborate on the reason why it was failing.

The node containers expect to connect to a hub which is resolvable as simply:

hub

rather than in their example where it will be resolvable as:

selenium_hub


回答2:

As a side note, if using docker-compose version 2 format you have to specify a couple env variables, otherwise the node will not connect to the hub:

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

Credits: Containers are not linked with docker-compose version 2



回答3:

Stumbling across this tutorial, there was this syntax provided. And even though it's similar to one of my approaches, it worked.

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

It's seems to be something about the naming, yet am not sure.