0

I am trying to run a Selenium script written in Python inside a Docker container via Selenium Grid. Unfortunately I can't manage to configure the remote webdriver.

This is the Docker Compose file:

version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.1.3-20220327
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:4.1.3-20220327
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:4.1.3-20220327
    container_name: selenium-hub
    ports:
      - "4444:4444"

  python-script:
    build: .

This is the webdriver setup within the Python code:

    driver = webdriver.Remote(
        desired_capabilities=DesiredCapabilities.FIREFOX,
        command_executor="http://localhost:4444/wd/hub"
    )
        

It works when I run the Python script locally with these settings. But as soon as I want to start it inside a Docker container, I get the following error, among others:

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

I'm totally new to docker and also quite new to programming itself, so help would be very very nice.

Thank you!

2
  • This error usually means that your internet is either too slow or is not working, or that you have some sort of firewall blocking the request. I get it with pip all the time. Commented Mar 30, 2022 at 20:12
  • The comment by @SylvesterKruin gave me the food for thought I needed. It wasn't the internet that was too slow, but the browser services inside the Docker container were slower set up than my script. Adding the appropriate dependency solved it. Thank you! Commented Mar 31, 2022 at 10:21

1 Answer 1

1

TLDR : Try this:

driver = webdriver.Remote(
    desired_capabilities=DesiredCapabilities.FIREFOX,
    command_executor="http://selenium-hub:4444/wd/hub"
)

The reason it works locally from vs-code, is that localhost points to your local machine. Your docker container has its own idea of what localhost means. When the code runs inside the container, localhost refers to that container. Is that container listening to that port? Probably not - and this is why it doesn't work. Docker has its own network stack!

What you want to contact is the other container "selenium-hub". In docker, the service-name (or container name) becomes the host - but this only works from within the docker network. (Docker-compose makes a default network for you you don't specify one)

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

1 Comment

I forgot : Selenium-hub service specifides ports : - "4444:4444". This means that localhost:4444 is mapped to selenium-hub:4444 when you are outside the docker network stack. This is why it works on your local machine

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.