Apple Silicon与Intel芯片上Docker化Python3.8+Selenium运行问题咨询
问题背景
我尝试在Docker容器中运行带有ChromeDriver+Selenium的Python3.8镜像,使用的requirements.txt如下:
requests~=2.27.1 pandas~=1.4.3 selenium~=4.3.0 webdriver-manager~=3.7.1
编写的Dockerfile如下:
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 requirements 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
使用命令docker build --platform amd64 -t <username>/<imagename>:<version>构建镜像(必须添加--platform amd64否则构建失败)。
遇到的问题
M1 MacBook(Apple Silicon)运行报错
容器内Chrome启动崩溃:
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:
Intel芯片Windows机器(Surface Laptop 4)运行报错
容器内出现Selenium弹窗错误(韩文提示):
[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)
咨询问题
- 能否在Apple Silicon机器上正常运行ChromeDriver+Selenium容器?
- 最终要在AWS EC2上运行,会出现和Windows机器一样的错误吗?
解答
问题1:Apple Silicon机器上能否正常运行ChromeDriver+Selenium容器?
可以,但需要调整配置,当前问题出在用amd64架构的Chrome在arm64机器上通过Rosetta2模拟运行,缺少无头模式的必要参数,且硬件模拟导致稳定性差。
解决步骤:
- 修改Chrome启动参数,添加无头模式及容器运行必需的参数,初始化WebDriver时配置:
from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--headless=new") # Selenium 4.3+推荐的新无头模式 chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--window-size=1920,1080") self.browser = webdriver.Chrome(options=chrome_options) - 可选优化:改用支持arm64架构的Chrome镜像,比如基于
python:3.8-slim-bullseye(arm64版本),安装arm版Chrome,避免Rosetta2模拟的性能损耗和兼容性问题。
问题2:AWS EC2上运行是否会出现Windows机器一样的错误?
不会出现相同的架构相关错误,但要注意两点:
- Windows机器上的错误是业务逻辑层面的弹窗提示(韩文翻译:未输入查询日期错误,请输入查询日期后操作,请确认查询日期[00020827]),这是代码访问目标网站时未正确输入查询日期导致的,和运行环境无关。
- AWS EC2如果选择amd64架构实例(如t2、t3系列),运行现有镜像时只要配置好Chrome的无头启动参数,就能正常启动Chrome;如果选择arm64架构实例(如m6g系列),则需要调整镜像为arm64版本,或添加问题1中的启动参数避免崩溃。
简单说:EC2是纯Linux架构环境,不会有Windows机器上的架构模拟问题,但你需要修复代码中未处理查询日期输入的逻辑,否则仍会遇到那个弹窗错误。
内容的提问来源于stack exchange,提问作者배상일
相关产品推荐
相关产品推荐

