2

I use the following docker-compose.yml to run jupyter notebook based on jupyter/datascience-notebook:87210526f381 and selenium/node-chrome:

    version: '3'
    services:
    selenium-hub:
        image: selenium/hub:3.141.59-dubnium
        container_name: selenium-hub
        ports:
        - "4444:4444"
    chrome:
        image: selenium/node-chrome:3.141.59-dubnium
        volumes:
        - /dev/shm:/dev/shm
        depends_on:
        - selenium-hub
        environment:
        - HUB_HOST=selenium-hub
        - HUB_PORT=4444
        networks: 
        - backend
    nbdatascience: 
        container_name: nbdatascience
        image: aabor/nbdatascience
        build: nbdatascience/.
        volumes:
        - /home/$USER/py:/home/jovyan/work/py
        - /home/$USER/.jupyter:/home/jovyan/.jupyter
        ports:
        - "10000:8888"
        environment:
        - TZ="Europe/Kiev"
        restart: always
        networks: 
        - backend
        depends_on:
        - chrome
    networks: 
    backend:
        driver: bridge

When all these containers up selenium hub is accessible at http://localhost:4444/, and jupyter lab at http://localhost:10000/lab.

I am trying to open web browser session from notebook executing the following python script:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME
driver = webdriver.Remote(command_executor='localhost:4444', desired_capabilities=cap)

which gives me error message: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4c17137278>: Failed to establish a new connection: [Errno 111] Connection refused',))

Correction: running this python script resolves the problem, driver is created and it is possible to navigate the Internet in headless mode:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME 
driver = webdriver.Remote(command_executor='http://selenium-hub:4444/wd/hub',desired_capabilities=cap)

How do I open connection to selenium chrome running in another docker container? The documentation in SeleniumHQ/docker-selenium lacks these details.

The documentation on docker network says that "Once connected, the containers can communicate using only another container’s IP address or name", so is it possible to call another container by name in python script, for example: driver = webdriver.Remote(command_executor='chrome', desired_capabilities=cap). I tried this command, but it gives me the same error: "connection refused".

1
  • selenium-hub has a lo interface different than localhost that are using the other containers, because they have declared backend network. So, try to add networks: - backend to selenium-hub. If it doesn't work, add network_mode: host to all of them. Commented Jan 16, 2019 at 13:05

1 Answer 1

3

Connect your selenium container to the same network backend and use selenium-hub:4444 as hostname instead of localhost:4444.

By the way, what's for do you declare network? It is created by docker-compose by default.

Also, there is no need to explicitly declare container_name - containers get the name of their service by default.

I suggest following changes:

docker-compose.yml

version: '3'
services:
  selenium-hub:
    image: selenium/hub:3.141.59-dubnium
    ports:
    - "4444:4444"
  chrome:
    image: selenium/node-chrome:3.141.59-dubnium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  nbdatascience: 
    image: aabor/nbdatascience
    build: nbdatascience/.
    volumes:
      - /home/$USER/py:/home/jovyan/work/py
      - /home/$USER/.jupyter:/home/jovyan/.jupyter
    ports:
      - "10000:8888"
    environment:
      - TZ="Europe/Kiev"
    restart: always
    depends_on:
      - chrome

Also, if you dont connect to containers from outside, remove ports exposing.

Sign up to request clarification or add additional context in comments.

3 Comments

I implemented your changes, but it still gives the error HTTPConnectionPool(host='chrome', port=4444): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f3e102be198>: Failed to establish a new connection: [Errno 111] Connection refused',))
Sorry, my bad, selenium-hub:4444. You should connect to selenium, right?
Finally, I managed to do it. Using your corrections to docker-compose.yml this python script passed cap = DesiredCapabilities.CHROME driver = webdriver.Remote(command_executor='http://selenium-hub:4444/wd/hub',desired_capabilities=cap). Chrome browser session opened in headless mode and I managed to download some pages from Internet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.