Skip to content

Commit 1ad9b3b

Browse files
committed
Detect running in container and add args
1 parent ef92ed6 commit 1ad9b3b

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

nodes/Puppeteer/Puppeteer.node.options.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
11
import { INodeTypeDescription, NodeConnectionType } from 'n8n-workflow';
2+
import { existsSync, readFileSync } from 'fs';
3+
4+
function isRunningInContainer(): boolean {
5+
try {
6+
// Method 1: Check for .dockerenv file
7+
if (existsSync('/.dockerenv')) {
8+
console.log('Puppeteer node: Container detected via .dockerenv file');
9+
return true;
10+
}
11+
12+
// Method 2: Check cgroup (Linux only)
13+
if (process.platform === 'linux') {
14+
try {
15+
const cgroupContent = readFileSync('/proc/1/cgroup', 'utf8');
16+
if (cgroupContent.includes('docker') || cgroupContent.includes('kubepods')) {
17+
console.log('Puppeteer node: Container detected via cgroup content');
18+
return true;
19+
}
20+
} catch (error) {
21+
console.log('Puppeteer node: cgroup check skipped');
22+
}
23+
}
24+
25+
// Method 3: Check common container environment variables
26+
if (process.env.KUBERNETES_SERVICE_HOST ||
27+
process.env.DOCKER_CONTAINER ||
28+
process.env.DOCKER_HOST) {
29+
console.log('Puppeteer node: Container detected via environment variables');
30+
return true;
31+
}
32+
33+
return false;
34+
} catch (error) {
35+
// If any error occurs during checks, log and return false
36+
console.log('Puppeteer node: Container detection failed:', (error as Error).message);
37+
return false;
38+
}
39+
}
240

341
/**
442
* Options to be displayed
@@ -683,6 +721,14 @@ export const nodeDescription: INodeTypeDescription = {
683721
description:
684722
'This tells Puppeteer to use a custom proxy configuration. Examples: localhost:8080, socks5://localhost:1080, etc.',
685723
},
724+
{
725+
displayName: 'Add Container Arguments',
726+
name: 'addContainerArgs',
727+
type: 'boolean',
728+
default: isRunningInContainer(),
729+
description: 'Whether to add recommended arguments for container environments (--no-sandbox, --disable-setuid-sandbox, --disable-dev-shm-usage, --disable-gpu)',
730+
required: false,
731+
},
686732
],
687733
},
688734
],

nodes/Puppeteer/Puppeteer.node.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ const {
3030
CODE_ENABLE_STDOUT,
3131
} = process.env;
3232

33+
const CONTAINER_LAUNCH_ARGS = [
34+
'--no-sandbox',
35+
'--disable-setuid-sandbox',
36+
'--disable-dev-shm-usage',
37+
'--disable-gpu'
38+
];
39+
3340
export const vmResolver = makeResolverFromLegacyOptions({
3441
external: external
3542
? {
@@ -223,6 +230,21 @@ export class Puppeteer implements INodeType {
223230
args.push(...launchArgs.map((arg: IDataObject) => arg.arg as string));
224231
}
225232

233+
const addContainerArgs = options.addContainerArgs === true;
234+
if (addContainerArgs) {
235+
// Only add container args that weren't already specified by the user
236+
const missingContainerArgs = CONTAINER_LAUNCH_ARGS.filter(arg => !args.some(
237+
existingArg => existingArg === arg || existingArg.startsWith(`${arg}=`)
238+
));
239+
240+
if (missingContainerArgs.length > 0) {
241+
console.log('Puppeteer node: Adding container optimizations:', missingContainerArgs);
242+
args.push(...missingContainerArgs);
243+
} else {
244+
console.log('Puppeteer node: Container optimizations already present in launch arguments');
245+
}
246+
}
247+
226248
// More on proxying: https://www.chromium.org/developers/design-documents/network-settings
227249
if (options.proxyServer) {
228250
args.push(`--proxy-server=${options.proxyServer}`);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "n8n-nodes-puppeteer",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "n8n node for browser automation using Puppeteer",
55
"license": "MIT",
66
"homepage": "https://github.com/drudge/n8n-nodes-puppeteer",

0 commit comments

Comments
 (0)