'Running chromium-chomedriver with Selenium in Docker container

I'm trying to run a Python application that uses Selenium and chromedriver inside a Docker container, but can't get it to work. It works with an image built from ubuntu:bionic, but not from ubuntu:focal

Dockerfile:

FROM ubuntu:focal  # works only with ubuntu:bionic

RUN apt-get -qq update
RUN DEBIAN_FRONTEND=noninteractive \
  apt-get install -y -qq \
  chromium-chromedriver
RUN apt-get install -y -qq python3-pip
RUN pip3 install -qq --upgrade pip
RUN pip3 install -qq --no-cache-dir selenium==3.141.0
COPY test.py ./

CMD python3 test.py

test.py:

from selenium.webdriver import Chrome, ChromeOptions


def main():
    options = ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--remote-debugging-port=9222')
    driver = Chrome('/usr/lib/chromium-browser/chromedriver',
                    options=options)
    driver.get('http://stackoverflow.com')
    print(driver.title)


if __name__ == '__main__':
    main()

Traceback:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    main()
  File "test.py", line 10, in main
    driver = Chrome('/usr/lib/chromium-browser/chromedriver',
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service /usr/lib/chromium-browser/chromedriver unexpectedly exited. Status code was: 1

Any help would be very appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source