Skip to content

Commit 43afef4

Browse files
authored
fix(docker): include globally installed puppeteer (#736)
1 parent 55fd467 commit 43afef4

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docs/recipes/docker-client/Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM node:14-bullseye-slim
22

3+
# Set variable so puppeteer will not try to download chromium
4+
ENV PUPPETEER_SKIP_DOWNLOAD=true
5+
36
# Install utilities
47
RUN apt-get update --fix-missing && apt-get -y upgrade && apt-get install -y git wget gnupg && apt-get clean
58

@@ -14,12 +17,17 @@ RUN apt-get update \
1417
RUN npm install -g @lhci/[email protected]
1518
RUN npm install -g lighthouse
1619

20+
# Install puppeteer
21+
RUN npm install -g puppeteer
22+
1723
# Setup a user to avoid doing everything as root
1824
RUN groupadd --system lhci && \
1925
useradd --system --create-home --gid lhci lhci && \
2026
mkdir --parents /home/lhci/reports && \
2127
chown --recursive lhci:lhci /home/lhci
2228

29+
RUN cd /home/lhci/reports && npm link puppeteer
30+
2331
USER lhci
2432
WORKDIR /home/lhci/reports
2533

docs/recipes/docker-client/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ docker container run --cpus=".9" --shm-size=2g --cap-add=SYS_ADMIN \
5454
```
5555
The above command will provide 0.9 CPU from the available CPUs. In a 2 core processor, docker will have access to 45% of the CPU cycles. You can tune it as per your need. Keep a check on CPU/Memory Power at the bottom of your lighthouse report for consistent results.
5656

57+
## Using puppeteer
58+
Puppeteer is present as a dependency in the docker container. If you specify a `puppeteerScript` when running, you should also provide 2 `puppeteerLaunchOptions` for it to work properly in the container, `--no-sandbox` and `--disable-setuid-sandbox`.
59+
60+
As CLI arguments:
61+
```bash
62+
docker container run --cap-add=SYS_ADMIN \
63+
-v "$(pwd)/lhci-data:/home/lhci/reports/.lighthouseci" \
64+
-v "$(pwd)/scripts:/home/lhci/reports/scripts" \
65+
patrickhulce/lhci-client \
66+
lhci collect --url="https://example.com" \
67+
--puppeteerScript=docs/recipes/puppeteer-example.js \
68+
--puppeteerLaunchOptions.args=--no-sandbox \
69+
--puppeteerLaunchOptions.args=--disable-setuid-sandbox
70+
```
71+
72+
When using `lighthouserc.js`:
73+
```javascript
74+
module.exports = {
75+
ci: {
76+
collect: {
77+
// ...
78+
puppeteerScript: 'docs/recipes/puppeteer-example.js',
79+
puppeteerLaunchOptions: {
80+
args: ['--no-sandbox', '--disable-setuid-sandbox']
81+
},
82+
// ...
83+
},
84+
// ...
85+
},
86+
};
87+
```
88+
5789
## `--no-sandbox` Issues Explained
5890

5991
Chrome uses sandboxing to isolate renderer processes and restrict their capabilities. If a rogue website is able to discover a browser vulnerability and break out of JavaScript engine for example, they would find themselves in a very limited process that can't write to the filesystem, make network requests, mess with devices, etc.

docs/recipes/puppeteer-example.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license Copyright 2022 Google Inc. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5+
*/
6+
'use strict';
7+
8+
/**
9+
* @param {puppeteer.Browser} browser
10+
* @param {{url: string, options: LHCI.CollectCommand.Options}} context
11+
*/
12+
module.exports = async (browser, context) => {
13+
// launch browser for LHCI
14+
const page = await browser.newPage();
15+
16+
await page.goto("https://example.com");
17+
18+
// close session for next run
19+
await page.close();
20+
};

0 commit comments

Comments
 (0)