M1 Mac Docker容器中undetected-chromedriver崩溃问题求助
解决M1 Mac Docker容器中undetected-chromedriver崩溃问题
问题症状
在M1 Mac的Alpine Docker容器中运行undetected-chromedriver时出现崩溃,抛出以下错误:
selenium.common.exceptions.WebDriverException: Message: Service /root/.local/share/undetected_chromedriver/undetected_chromedriver unexpectedly exited. Status code was: 255
普通的webdriver.Chrome()可以正常运行,但undetected版本无法启动。
解决方案
1. 修复Dockerfile依赖与版本适配
Alpine镜像的musl libc与Chrome依赖的glibc存在兼容性问题,同时undetected-chromedriver默认会下载适配x86的ChromeDriver,导致M1容器运行失败。修改Dockerfile如下:
# 使用支持arm64的Python Alpine镜像 FROM python:3.10-alpine3.18 # 安装Chrome及必要依赖(适配arm64) RUN apk update && apk add --no-cache \ chromium \ chromium-chromedriver \ glib \ nss \ freetype \ harfbuzz \ ca-certificates \ ttf-freefont # 设置环境变量,让undetected-chromedriver使用系统安装的Chrome/ChromeDriver ENV CHROME_BIN=/usr/bin/chromium-browser ENV CHROME_DRIVER=/usr/bin/chromedriver # 升级pip并安装依赖 RUN pip install --upgrade pip COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt WORKDIR /app COPY . /app CMD ["python3", "-u", "main.py"]
2. 更新ChromeOptions配置
添加更多适配容器环境的参数,尤其是针对headless模式和arm64架构的优化:
def set_undetected_chrome_options() -> uc.ChromeOptions: chrome_options = uc.ChromeOptions() # 使用新版headless模式,兼容性更好 chrome_options.add_argument("--headless=new") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-blink-features=AutomationControlled") # 指定Chrome二进制文件路径 chrome_options.binary_location = "/usr/bin/chromium-browser" # 禁用GPU,避免容器中无GPU导致的错误 chrome_options.add_argument("--disable-gpu") # 禁用扩展 chrome_options.add_argument("--disable-extensions") # 设置窗口大小,避免页面布局异常 chrome_options.add_argument("--window-size=1920,1080") return chrome_options
3. 初始化undetected-chromedriver时指定系统驱动路径
避免undetected-chromedriver自动下载不兼容的驱动版本,直接使用容器中安装的chromedriver:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By def scrape_pararius(logger): options = set_undetected_chrome_options() driver = uc.Chrome( options=options, driver_executable_path="/usr/bin/chromedriver", browser_executable_path="/usr/bin/chromium-browser", # 禁止自动更新驱动 auto_update=False ) try: driver.get(SEARCH_URL) # 替换time.sleep为显式等待,更可靠 WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "search-list__item")) ) page_source = driver.page_source soup = BeautifulSoup(page_source, "html.parser") properties = soup.find_all("li", class_="search-list__item search-list__item--listing") finally: driver.quit()
4. 验证权限设置
确保容器内的Chrome可以正常运行,避免权限问题:
- 在Dockerfile中添加
RUN chmod +x /usr/bin/chromium-browser /usr/bin/chromedriver(如果需要) - 运行容器时可以临时添加
--user root排查权限问题(生产环境不推荐)
关键说明
- M1 Mac是arm64架构,必须确保Docker镜像和安装的Chrome/ChromeDriver支持arm64,Alpine 3.18及以上版本对arm64的支持更好
- undetected-chromedriver默认会下载x86版本的驱动,因此必须强制指定系统安装的arm64驱动路径
- 新版headless模式(
--headless=new)比旧模式更稳定,避免很多兼容性问题
内容的提问来源于stack exchange,提问作者Martin Hristev
相关产品推荐
相关产品推荐

