How to change port of selenium/hub docker container?
Asked Answered
B

4

6

I am executing automation tests using Docker containers. I have to run test suites for multiple applications on the same server. But if I have same port for each selenium hub docker container then I cannot run all these suites at the same time. Thus I want to assign different ports to each selenium/hub docker container. Is there any way I can change hub container's port? Or do I need to write my own dockerfile and not use selenium/hub docker images?

My docker-compose file looks like this

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
Birthstone answered 20/6, 2018 at 22:18 Comment(1)
Why ports are in pair ? ports: - "4444:4444"Crackleware
L
6

You can change the ports using the SE_OPTS environment variable: just add

environment:
  SE_OPTS: "-port <YOUR_PREFERED_PORT>"

to your docker-compose.yml and Selenium will start at <YOUR_PREFERED_PORT>.

See https://github.com/SeleniumHQ/docker-selenium#se_opts-selenium-configuration-options

Lorgnon answered 30/7, 2018 at 8:55 Comment(0)
D
0

According to the Dockerfile https://github.com/SeleniumHQ/docker-selenium/blob/master/Hub/Dockerfile you can set GRID_HUB_PORT

    environment:
      GRID_HUB_PORT: "4545"
Decoder answered 17/7, 2020 at 8:13 Comment(0)
S
0

Just do a find and replace of 4444 with whatever port you want to use. For example, use 4440 instead of 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
Sewell answered 24/9, 2020 at 17:21 Comment(0)
A
0

I had a situation where I was conducting automated selenium tests for two different web applications on the same Jenkins machine. I needed 2 selenium grids to be set up and running on the same machine each employing unique and distinct ports. Instead of creating docker images of my own for the hubs and nodes I changed the docker compose file for the second selenium grid to the following:


version: "3.5"
services:

  hub:
    image: selenium/hub
    container_name: selenium_hub_nia
    ports:
      - "3333:4444"
    networks:
      - nia_bridge

    environment:
      GRID_MAX_SESSION: 16
      GRID_BROWSER_TIMEOUT: 10000
      GRID_TIMEOUT: 10000
      GRID_HUB_PORT: 3333
    expose:
      - "3333"

  chrome:
    image: selenium/node-chrome
    container_name: selenium_node_nia_chrome
    depends_on:
      - hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=hub
      - HUB_PORT_4444_TCP_PORT=3333
      - NODE_MAX_SESSION=4
      - NODE_MAX_INSTANCES=4
    volumes:
      - /dev/shm:/dev/shm
    ports:
      - "9003:5900"
    links:
      - hub
    networks:
      - nia_bridge

  firefox:
    image: selenium/node-firefox-debug
    container_name: selenium_node_nia_firefox
    depends_on:
      - hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=hub
      - HUB_PORT_4444_TCP_PORT=3333
      - NODE_MAX_SESSION=4
      - NODE_MAX_INSTANCES=4
    volumes:
      - /dev/shm:/dev/shm
    ports:
      - "9004:5900"
    links:
      - hub
    networks:
      - nia_bridge
networks:
    nia_bridge: {}

Three major changes in the above file:

  1. In the hub definition :
    I first mapped the port 4444 of the second selenium hub container to the host (Jenkins server) port 3333 (the first hub continues to run on 4444). This solves the error of 4444 being bound already when the first selenium hub container is running. I also defined its GRID_HUB_PORT as 3333 and exposed that port to the containers on the network bridge named nia-bridge.
  2. In the two services for my 2 nodes (chrome and firefox), I specified the HUB_PORT_4444_TCP_PORT as 3333 so they use the correct url to register to the hub.
  3. This docker compose file spins up 3 containers. I made the container names unique i.e. different from the pre-existing selenium grid network that you may already have for your other web application. This is required because docker container names must be unique. The above helped me spin up a completely distinct selenium grid with the hub and 2 nodes running on ports 3333,9003,9004 respectively of the host Jenkins server.
Atonality answered 20/9, 2021 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.