Home > front end >  Running dockerized python3.8 image with selenium on Apple silicon vs. Intel Chip
Running dockerized python3.8 image with selenium on Apple silicon vs. Intel Chip

Time:09-23

I'm trying to run a python3.8 image with following requirements.txt. My goal is to run chrome driver selenium on a dockerized container.

requests~=2.27.1
pandas~=1.4.3
selenium~=4.3.0
webdriver-manager~=3.7.1

I built the Dockerfile like so

FROM python:3.8

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# copy the requiremnets
COPY requirements.txt ./

# upgrade pip and install requirements
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# copy rest of the file
COPY . ./

EXPOSE 8001

# set display port to avoid crash
ENV DISPLAY=:99

and ultimately built the docker image using the following command line

docker build --platform amd64 -t <username>/<imagename>:<version>

I had to add --platform amd64 since it docker build would fail.

However, it failed to run on M1 Macbook: which threw me an error like so. This is the error message that I extracted inside of the docker container - docker on a apple silicon machine.

  File "process_fbond.py", line 149, in <module>
    fb = FBond()
  File "process_fbond.py", line 27, in __init__
    self.browser = webdriver.Chrome(.                         # <<< Chrome failed to load on to self.browser

(...)

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (chrome not reachable)                                      # <<< Chrome driver crashed
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:

I suspected a architect issue and tried the same thing on a windows machine with an Intel chip (surface laptop 4) - where it was built successfully. This is again the error I extracted inside of the docker container - on a windows machine.

Forgive me the Korean characters.

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 105.0.5195
[WDM] - Get LATEST chromedriver version for 105.0.5195 google-chrome
[WDM] - There is no [linux64] chromedriver for browser 105.0.5195 in cache
[WDM] - About to download new driver from https://chromedriver.storage.googleapis.com/105.0.5195.52/chromedriver_linux64.zip
[WDM] - Driver has been saved in cache [/root/.wdm/drivers/chromedriver/linux64/105.0.5195.52]
Traceback (most recent call last):
  File "process_fbond.py", line 154, in <module>
    result = fb.historic_run(s, f)
  File "process_fbond.py", line 129, in historic_run
    bond = self.search_rows()
  File "process_fbond.py", line 49, in search_rows
    r = self.browser.find_element(
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 246, in check_response
    raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: 조회일자 미입력 오류입니다.
조회일자 입력 후 거래해주세요.
조회일자[00020827]를 확인바랍니다.
Message: unexpected alert open: {Alert text : 조회일자 미입력 오류입니다.
조회일자 입력 후 거래해주세요.
조회일자[00020827]를 확인바랍니다.}             # <<< At least it gives me an selenium error
  (Session info: headless chrome=105.0.5195.125)

So my question is:

  1. Is there any way to get chrome-driver and selenium container running on an apple-silicon machine?
  2. My ultimate goal is to run this whole thing on a AWS EC2 machine. Would EC2 machine throw out the same error with the windows-machine one?

CodePudding user response:

I've been able to get Chrome Web Driver working in a Windows Docker container. You may need to do the same thing on the Linux side.

  • Make sure you have two arguments in your webdriver "--no-sandbox" and "--headless". Make sure no sandbox is first.
  • You may need to install fonts for the default fonts that Chrome uses, such as Arial, Times, etc. Believe it or not, it wouldn't work until I did that.
  • Ensure that the ChromeWebDriver and Chrome both have the same major version.
  • Declare 2g of shared memory in your Docker run statement. Whenever you run a Selenium test, about 5 instances of Chrome and the ChromeWebDriver get created. I've seen the amount of RAM go up to about 600MB believe it or not. This will quickly go away if you use the driver.quit statement after your test is done.
  • Related