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

Mac M1 ARM架构下Docker中Selenium+ChromeDriver使用故障排查

在Docker中使用Selenium Chrome浏览器的问题解决

问题场景

启动Docker化的Selenium Chrome节点命令:

docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:latest

使用Kotlin代码通过RemoteWebDriver连接:

val chromeOptions = ChromeOptions()
chromeOptions.addArguments("--no-sandbox")
chromeOptions.addArguments("--headless");
val url = URL("http://localhost:4444/wd/hub")
webDriver = RemoteWebDriver(url, chromeOptions)
val baseUrl = "https://www.google.com"
webDriver.get(baseUrl)
webDriver.quit()

遇到的错误

首次运行返回500错误,栈跟踪如下:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 
Host info: host: 'c3d9edcf7df3', ip: '172.17.0.2'
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.5.1', java.version: '11.0.16.1'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}]}]
Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
        at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)

尝试升级Selenium版本(从4.8.1到4.12.1),并添加--disable-dev-shm-usage和--remote-debugging-port=9222参数后,错误变为:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
  (chrome not reachable)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 
Host info: host: '419674405ad2', ip: '172.17.0.2'
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.5.1', java.version: '11.0.16.1'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --no-sandbox, --disable-dev-shm-usage, --headless, --disable-extensions, --remote-debugging-port=9222], extensions: []}}]}]
Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --no-sandbox, --disable-dev-shm-usage, --headless, --disable-extensions, --remote-debugging-port=9222], extensions: []}}
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
        at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:163)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:143)

解决步骤

1. 调整Chrome启动参数

核心问题是旧版--headless参数的兼容性,以及缺少必要的权限和配置参数。修改后的ChromeOptions如下:

val chromeOptions = ChromeOptions()
// 使用Chrome 112+推荐的新无头模式,行为更接近正常浏览器
chromeOptions.addArguments("--headless=new")
// 关闭沙箱,Docker容器内无足够权限运行沙箱
chromeOptions.addArguments("--no-sandbox")
// 禁用/dev/shm,避免Docker默认小共享内存导致崩溃
chromeOptions.addArguments("--disable-dev-shm-usage")
// 允许任意来源连接,解决Selenium 4+的跨域限制
chromeOptions.addArguments("--remote-allow-origins=*")
// 无头模式下禁用GPU加速
chromeOptions.addArguments("--disable-gpu")

val url = URL("http://localhost:4444/wd/hub")
webDriver = RemoteWebDriver(url, chromeOptions)
val baseUrl = "https://www.google.com"
webDriver.get(baseUrl)
webDriver.quit()

2. 匹配Selenium版本与Docker镜像

避免使用latest镜像,改用与本地依赖一致的版本,减少兼容性问题。比如本地用4.12.1,启动命令改为:

# 苹果芯片(M1/M2)需添加--platform参数指定架构
docker run -d --platform linux/amd64 -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:4.12.1

3. 验证容器状态

运行docker logs <容器ID>查看容器内日志,确认Chrome节点是否正常注册,排查启动时的具体错误(如权限、端口冲突等)。

4. 额外检查

  • 确保本地端口4444、7900未被其他程序占用
  • 若使用防火墙,允许本地访问这两个端口

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:54:59