You need to enable JavaScript to run this app. Lambda Docker使用chrome-aws-lambda+Lighthouse报9222连接拒绝错误
优惠活动
大模型
产品
解决方案
定价
更多

Lambda Docker使用chrome-aws-lambda+Lighthouse报9222连接拒绝错误

Lambda Node.js 12容器环境集成Lighthouse性能检测报9222连接拒绝问题

场景说明

在基于Lambda官方Node.js 12版本Docker镜像搭建的运行环境中,集成chrome-aws-lambda、puppeteer-core、Lighthouse组件实现页面性能检测功能时,始终触发127.0.0.1:9222连接拒绝的固定报错,调整各类配置后问题无改善。

相关配置与复现步骤

1. Dockerfile 配置

FROM public.ecr.aws/lambda/nodejs:12

COPY index.js package.json /var/task/

RUN npm install --save-prod

CMD [ "index.handler" ]

2. package.json 依赖配置

{
    "name": "lighthouse2",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "chrome-aws-lambda": "^7.0.0",
        "puppeteer-core": "^7.0.4",
        "lighthouse": "^7.1.0"
    }
}

3. 业务入口代码 index.js

const chromium = require('chrome-aws-lambda');
const lighthouse = require('lighthouse');

exports.handler = async(event, context, callback) => {
    let result = null;
    let chrome = null;

    try {
        console.log('启动puppeteer chromium浏览器');
        let chromiumOptions = {
            args: chromium.args,
            defaultViewport: chromium.defaultViewport,
            executablePath: await chromium.executablePath,
            headless: chromium.headless,
            ignoreHTTPSErrors: true
        };
        const command = await chromium.puppeteer.launch(chromiumOptions)
            .then((chrome) => {
                console.log('puppeteer启动成功');
                return {
                    chrome,
                    async start() {
                        console.log('开始执行检测任务');

                        const options = {logLevel: 'info', onlyCategories: ['performance']};
                        const runnerResult = await lighthouse('https://www.example.com', options)

                        console.log('报告生成完成,检测地址:', runnerResult.lhr.finalUrl);
                        console.log('性能得分:', runnerResult.lhr.categories.performance.score * 100);

                        await chrome.kill();
                    }
                }
            })
            .catch(err => console.error(err));

        console.log('浏览器初始化完成');

        return await command.start();
    } catch (error) {
        console.log(error);
        return callback(error);
    } finally {
        if (chrome !== null) {
            await chrome.close();
        }
    }

    return callback(null, result);
};

4. 本地测试操作

  • 构建测试镜像
    docker build -t lighthouse2 .
    
  • 启动本地测试容器
    docker run --rm -p 9000:8080 lighthouse2
    
  • 在另一终端发起函数调用测试
    curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
    

运行结果对比

预期效果

Lighthouse可正常执行检测流程,顺利读取性能报告的各项属性值。

实际报错

无论调整何种配置,Lighthouse运行阶段均触发以下固定报错:

Wed, 17 Feb 2021 23:24:18 GMT status Connecting to browser
Wed, 17 Feb 2021 23:24:18 GMT CriConnection:warn Cannot create new tab; reusing open tab.
Wed, 17 Feb 2021 23:24:18 GMT status Disconnecting from browser...
Wed, 17 Feb 2021 23:24:18 GMT CriConnection:error sendRawMessage() was called without an established connection.
Wed, 17 Feb 2021 23:24:18 GMT GatherRunner disconnect:error sendRawMessage() was called without an established connection.
Error: connect ECONNREFUSED 127.0.0.1:9222
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 9222
}
2021-02-17T23:24:18.145Z        8cd07390-6550-40b7-87a5-c162fc55eedf    ERROR   Invoke Error    {"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:9222","code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":9222,"stack":["Error: connect ECONNREFUSED 127.0.0.1:9222","    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)"]}

内容的提问来源于stack exchange,提问作者Tyler V.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.03 22:15:06