You need to enable JavaScript to run this app. Laravel Dusk + Laradock环境下Chrome启动崩溃问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Dusk + Laradock环境下Chrome启动崩溃问题求助

Laradock环境下Laravel Dusk启动Chrome失败的解决建议

问题场景

在Laradock环境中运行Laravel Dusk测试时出现Chrome启动崩溃错误,报错信息如下:

  1. Tests\Browser\ExampleTest::testBasicExample
    Facebook\WebDriver\Exception\UnknownErrorException: unknown error: Chrome failed to start: crashed.
    (unknown error: DevToolsActivePort file doesn't exist)
    (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

环境信息

  • Google Chrome 112.0.5615.121
  • ChromeDriver binary version 112.0.5615.49
  • Windows 11 WSL 2
  • Laravel 9.23
  • Laravel Dusk 7.7
  • PHP 8.1

解决建议

1. 调整Chrome启动参数

修改tests/DuskTestCase.php中的driver方法,添加适配容器/WSL环境的启动参数:

protected function driver()
{
    $options = (new ChromeOptions)->addArguments([
        '--headless=new',
        '--disable-gpu',
        '--no-sandbox',
        '--disable-dev-shm-usage',
        '--remote-debugging-port=9222',
    ]);

    return RemoteWebDriver::create(
        'http://localhost:9515',
        DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY,
            $options
        )
    );
}

其中--no-sandbox和--disable-dev-shm-usage是解决容器/WSL中Chrome崩溃的核心参数,--remote-debugging-port可解决DevToolsActivePort文件缺失问题。

2. 确保容器内Chrome与ChromeDriver版本完全匹配

进入Laradock的workspace容器,重装对应版本的Chrome和ChromeDriver:

# 进入workspace容器
docker-compose exec workspace bash

# 卸载旧版Chrome
apt remove --purge google-chrome-stable -y

# 安装指定版本Chrome
wget https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_112.0.5615.121-1_amd64.deb
dpkg -i google-chrome-stable_112.0.5615.121-1_amd64.deb
apt install -f -y

# 下载并配置对应版本ChromeDriver
wget https://chromedriver.storage.googleapis.com/112.0.5615.49/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
mv chromedriver /usr/local/bin/

3. 优化WSL2资源配置

WSL2默认内存限制可能导致Chrome崩溃,在Windows的C:\Users\<你的用户名>路径下创建.wslconfig文件:

[wsl2]
memory=4GB
processors=2
swap=2GB

配置完成后执行wsl --shutdown重启WSL,再重新启动Laradock容器。

4. 配置Laradock的Selenium服务

修改Laradock的docker-compose.yml,启用并配置Selenium服务:

selenium:
  image: selenium/standalone-chrome:112.0
  volumes:
    - /dev/shm:/dev/shm
  ports:
    - "4444:4444"
    - "7900:7900"
  environment:
    - SE_NODE_MAX_SESSIONS=10
    - SE_NODE_OVERRIDE_MAX_SESSIONS=true

重启Selenium服务:

docker-compose restart selenium

同时修改Dusk的驱动配置,连接Selenium服务:

protected function driver()
{
    $options = (new ChromeOptions)->addArguments([
        '--headless=new',
        '--disable-gpu',
        '--no-sandbox',
    ]);

    return RemoteWebDriver::create(
        'http://selenium:4444/wd/hub',
        DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY,
            $options
        )
    );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 05:22:27