You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中Chromium版本过旧致Selenium渲染失败问题排查

Docker中Chromium版本过旧导致Selenium渲染超时问题

我正在构建一个安装Chromium和chromium-driver的Docker镜像,但运行Python脚本时始终因渲染问题崩溃,报错显示使用的是headless chrome=89.0.4389.114。但直接在树莓派上安装106.0.5249.119版本的Chromium和chromium-driver时,脚本运行正常。请问为何Dockerfile会安装旧版本?


Dockerfile内容

FROM python:3.9.2

RUN apt-get update

# install chromium
RUN apt-get install -y chromium

# install chromium-driver
RUN apt-get install chromium-driver

RUN /usr/local/bin/python -m pip install --upgrade pip
COPY . /data
WORKDIR /data
RUN pip install --no-cache-dir -r requirements.txt

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

# custom env vars
ENV DOCKER_IMAGE=true

报错信息

Exception in thread Thread-5:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/data/****.py", line 799, in main_function
    browser = browserSetup(True, PC_USER_AGENT)
  File "/data/****.py", line 42, in browserSetup
    chrome_browser_obj = webdriver.Chrome(options=options)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
    super().__init__(
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 272, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 364, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from timeout: Timed out receiving message from renderer: 600.000
  (Session info: headless chrome=89.0.4389.114)

浏览器初始化Python脚本

from selenium.webdriver.chrome.options import Options
options = Options()
prGreen("[DOCKER] Setting flags for Docker")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("user-agent=" + user_agent)
options.add_argument('lang=' + LANG.split("-")[0])
if headless_mode :
    options.add_argument("--headless")
options.add_argument('log-level=3')
chrome_browser_obj = webdriver.Chrome(options=options)
return chrome_browser_obj

问题原因及解决方法

为什么Docker里安装的是旧版本Chromium?

你使用的python:3.9.2基础镜像基于Debian 10(Buster),Debian稳定版的软件源会锁定软件大版本,Buster源中的Chromium停留在89.x版本。而树莓派使用的Raspberry Pi OS(基于Debian但有独立软件源维护)会提供更新的Chromium版本,因此直接在树莓派上能安装到106.x。

解决方法

1. 更换更新的基础镜像

使用基于Debian 12(Bookworm)或11(Bullseye)的Python镜像,这些版本的源包含更新的Chromium:

# 保留Python3.9的同时使用新Debian版本
FROM python:3.9-slim-bookworm

2. 为Debian Buster添加backports源(不换基础镜像的情况下)

在Dockerfile中添加backports源,从该源安装新版本Chromium:

RUN echo "deb http://deb.debian.org/debian buster-backports main" >> /etc/apt/sources.list
RUN apt-get update
# 从backports安装Chromium和对应driver
RUN apt-get install -y -t buster-backports chromium chromium-driver

3. 优化Selenium启动参数

旧版Chromium的headless模式存在渲染稳定性问题,即使升级版本后,也建议补充以下参数:

if headless_mode :
    # Chrome 112+推荐使用--headless=new替代旧的--headless
    options.add_argument("--headless=new")
# 添加禁用GPU和指定窗口尺寸参数
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1920,1080')

内容的提问来源于stack exchange,提问作者Tommaso Ascani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:30:49