Issue Running Google Lighthouse in Docker Container

Viewed 799

I'm attempting to run lighthouse in a Docker container for eventual deployment to AWS Lambda (using its new docker image lambda deployments). However I'm getting an error that I can't seem to understand.

The following is my dockerfile

FROM amazon/aws-lambda-nodejs:12

ENV AWS_LAMBDA_FUNCTION_MEMORY_SIZE=10240

RUN curl https://intoli.com/install-google-chrome.sh | bash

copy package.json .

RUN npm install

COPY app.js .

CMD ["app.handler"]

The following is my app.js

const chromeLauncher = require("chrome-launcher");
const lighthouse = require("lighthouse");

exports.handler = async (event) => {
  const chrome = await chromeLauncher.launch({
    logLevel: "info",
    chromeFlags: [
      "--headless",
      "--no-sandbox",
      "--disable-gpu",
      "--disable-dev-shm-usage",
      "--single-process",
    ],
  });

  const results = await lighthouse("https://example.com", {
    port: chrome.port,
    disableStorageReset: true,
    onlyCategories: ["performance"],
    logLevel: "info",
  });

  return {
    statusCode: 200,
    results,
  };
};

and the following is the log output. It seems to be able to connect to lighthouse but then is hanging on the first command it sends/

START RequestId: c9d7a07b-a5e2-4d03-8bf5-d0b5d248e3e7 Version: $LATEST
Sun, 24 Jan 2021 16:00:56 GMT ChromeLauncher Waiting for browser.
Sun, 24 Jan 2021 16:00:56 GMT ChromeLauncher Waiting for browser...
Sun, 24 Jan 2021 16:00:57 GMT ChromeLauncher Waiting for browser.....
Sun, 24 Jan 2021 16:00:57 GMT ChromeLauncher Waiting for browser.....✓
Sun, 24 Jan 2021 16:00:58 GMT status Connecting to browser
Sun, 24 Jan 2021 16:00:58 GMT status Resetting state with about:blank
Sun, 24 Jan 2021 16:01:28 GMT status Disconnecting from browser...
2021-01-24T16:01:28.172Z        c9d7a07b-a5e2-4d03-8bf5-d0b5d248e3e7    ERROR      Invoke Error    {"errorType":"LHError","errorMessage":"PROTOCOL_TIMEOUT","code":"PROTOCOL_TIMEOUT","name":"LHError","friendlyMessage":"Waiting for DevTools protocol response has exceeded the allotted time. (Method: Network.enable)","lhrRuntimeError":true,"protocolMethod":"Network.enable","stack":["LHError: PROTOCOL_TIMEOUT","    at Timeout._onTimeout (/var/task/node_modules/lighthouse/lighthouse-core/gather/driver.js:409:21)","    at listOnTimeout (internal/timers.js:554:17)","    at processTimers (internal/timers.js:497:7)"]}
END RequestId: c9d7a07b-a5e2-4d03-8bf5-d0b5d248e3e7
REPORT RequestId: c9d7a07b-a5e2-4d03-8bf5-d0b5d248e3e7  Init Duration: 3.53 ms     Duration: 33422.31 ms   Billed Duration: 33500 ms       Memory Size: 2010240 MB    Max Memory Used: 2010240 MB
1 Answers

To run on AWS Function Lambda you should use

    const chromium = require("chrome-aws-lambda");
    const chrome = await chromium.puppeteer.launch({
    args: [
      ...chromium.args,
      "--disable-dev-shm-usage",
      "--remote-debugging-port=9222",
    ],
    defaultViewport: chromium.defaultViewport,
    executablePath: await chromium.executablePath,
    headless: chromium.headless,
    ignoreHTTPSErrors: true,
});
const options = {
    logLevel: "info",
    output: "html",
    onlyCategories: ["performance"],
    preset: "mobile",
    port: 9222,
  };
  const runnerResult = await lighthouse(`${body.url}`, options);

But, I'm facing errors in the performance category, there is no way to make it works, All the others categories work fine.

Related