如何更改Selenium / hub Docker容器的端口?

时间:2018-06-20 22:18:51

标签: selenium docker docker-compose automated-tests selenium-grid

我正在使用Docker容器执行自动化测试。我必须在同一服务器上为多个应用程序运行测试套件。但是,如果每个selenium hub docker容器都具有相同的端口,则无法同时运行所有这些套件。因此,我想为每个selenium / hub docker容器分配不同的端口。有什么办法可以更改集线器容器的端口?还是我需要编写自己的dockerfile而不使用Selenium / hub Docker映像?

我的docker-compose文件看起来像这样

version: "3"
services:
  selenium-hub:
    restart: always
    image: selenium/hub:latest
    ports:
      - "4444:4444"
    environment:
      - GRID_BROWSER_TIMEOUT=300
      - GRID_TIMEOUT=300
selenium-chrome:
  restart: always
  image: selenium/node-chrome:latest
  depends_on:
    - selenium-hub
  volumes:
    - /dev/shm:/dev/shm
  links:
    - selenium-hub:hub
  environment:
    - HUB_PORT_4444_TCP_ADDR=selenium-hub
    - HUB_PORT_4444_TCP_PORT=4444
    - JAVA_OPT=-Xmx512m
    - DBUS_SESSION_BUS_ADDRESS=/dev/null
    - no_proxy=localhost
    - HUB_ENV_no_proxy=localhost
    - GRID_BROWSER_TIMEOUT=300
    - GRID_TIMEOUT=300
selenium-firefox:
  restart: always
  image: selenium/node-firefox:latest
  depends_on:
    - selenium-hub
  volumes:
    - /dev/shm:/dev/shm
  links:
    - selenium-hub:hub
  environment:
    - HUB_PORT_4444_TCP_ADDR=selenium-hub
    - HUB_PORT_4444_TCP_PORT=4444
    - JAVA_OPT=-Xmx512m
    - DBUS_SESSION_BUS_ADDRESS=/dev/null
    - no_proxy=localhost
    - HUB_ENV_no_proxy=localhost
    - GRID_BROWSER_TIMEOUT=300
    - GRID_TIMEOUT=300

3 个答案:

答案 0 :(得分:0)

您可以使用SE_OPTS环境变量来更改端口:只需添加

environment:
  SE_OPTS: "-port <YOUR_PREFERED_PORT>"

到您的docker-compose.yml,Selenium将从<YOUR_PREFERED_PORT>开始。

请参见https://github.com/SeleniumHQ/docker-selenium#se_opts-selenium-configuration-options

答案 1 :(得分:0)

根据Dockerfile https://github.com/SeleniumHQ/docker-selenium/blob/master/Hub/Dockerfile,您可以设置GRID_HUB_PORT

    environment:
      GRID_HUB_PORT: "4545"

答案 2 :(得分:0)

只需使用您想要使用的任何端口查找并替换4444。例如,使用4440而不是4444。

version: "3"
services:
  selenium-hub:
    restart: always
    image: selenium/hub:latest
    ports:
      - "4440:4440"
    environment:
      - GRID_BROWSER_TIMEOUT=300
      - GRID_TIMEOUT=300
selenium-chrome:
  restart: always
  image: selenium/node-chrome:latest
  depends_on:
    - selenium-hub
  volumes:
    - /dev/shm:/dev/shm
  links:
    - selenium-hub:hub
  environment:
    - HUB_PORT_4440_TCP_ADDR=selenium-hub
    - HUB_PORT_4440_TCP_PORT=4440
    - JAVA_OPT=-Xmx512m
    - DBUS_SESSION_BUS_ADDRESS=/dev/null
    - no_proxy=localhost
    - HUB_ENV_no_proxy=localhost
    - GRID_BROWSER_TIMEOUT=300
    - GRID_TIMEOUT=300
selenium-firefox:
  restart: always
  image: selenium/node-firefox:latest
  depends_on:
    - selenium-hub
  volumes:
    - /dev/shm:/dev/shm
  links:
    - selenium-hub:hub
  environment:
    - HUB_PORT_4440_TCP_ADDR=selenium-hub
    - HUB_PORT_4440_TCP_PORT=4440
    - JAVA_OPT=-Xmx512m
    - DBUS_SESSION_BUS_ADDRESS=/dev/null
    - no_proxy=localhost
    - HUB_ENV_no_proxy=localhost
    - GRID_BROWSER_TIMEOUT=300
    - GRID_TIMEOUT=300