Skip to content

Commit

Permalink
Detect running in container and add args
Browse files Browse the repository at this point in the history
  • Loading branch information
drudge committed Dec 7, 2024
1 parent ef92ed6 commit 1ad9b3b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
46 changes: 46 additions & 0 deletions nodes/Puppeteer/Puppeteer.node.options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
import { INodeTypeDescription, NodeConnectionType } from 'n8n-workflow';
import { existsSync, readFileSync } from 'fs';

function isRunningInContainer(): boolean {
try {
// Method 1: Check for .dockerenv file
if (existsSync('/.dockerenv')) {
console.log('Puppeteer node: Container detected via .dockerenv file');
return true;
}

// Method 2: Check cgroup (Linux only)
if (process.platform === 'linux') {
try {
const cgroupContent = readFileSync('/proc/1/cgroup', 'utf8');
if (cgroupContent.includes('docker') || cgroupContent.includes('kubepods')) {
console.log('Puppeteer node: Container detected via cgroup content');
return true;
}
} catch (error) {
console.log('Puppeteer node: cgroup check skipped');
}
}

// Method 3: Check common container environment variables
if (process.env.KUBERNETES_SERVICE_HOST ||
process.env.DOCKER_CONTAINER ||
process.env.DOCKER_HOST) {
console.log('Puppeteer node: Container detected via environment variables');
return true;
}

return false;
} catch (error) {
// If any error occurs during checks, log and return false
console.log('Puppeteer node: Container detection failed:', (error as Error).message);
return false;
}
}

/**
* Options to be displayed
Expand Down Expand Up @@ -683,6 +721,14 @@ export const nodeDescription: INodeTypeDescription = {
description:
'This tells Puppeteer to use a custom proxy configuration. Examples: localhost:8080, socks5://localhost:1080, etc.',
},
{
displayName: 'Add Container Arguments',
name: 'addContainerArgs',
type: 'boolean',
default: isRunningInContainer(),
description: 'Whether to add recommended arguments for container environments (--no-sandbox, --disable-setuid-sandbox, --disable-dev-shm-usage, --disable-gpu)',
required: false,
},
],
},
],
Expand Down
22 changes: 22 additions & 0 deletions nodes/Puppeteer/Puppeteer.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const {
CODE_ENABLE_STDOUT,
} = process.env;

const CONTAINER_LAUNCH_ARGS = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu'
];

export const vmResolver = makeResolverFromLegacyOptions({
external: external
? {
Expand Down Expand Up @@ -223,6 +230,21 @@ export class Puppeteer implements INodeType {
args.push(...launchArgs.map((arg: IDataObject) => arg.arg as string));
}

const addContainerArgs = options.addContainerArgs === true;
if (addContainerArgs) {
// Only add container args that weren't already specified by the user
const missingContainerArgs = CONTAINER_LAUNCH_ARGS.filter(arg => !args.some(
existingArg => existingArg === arg || existingArg.startsWith(`${arg}=`)
));

if (missingContainerArgs.length > 0) {
console.log('Puppeteer node: Adding container optimizations:', missingContainerArgs);
args.push(...missingContainerArgs);
} else {
console.log('Puppeteer node: Container optimizations already present in launch arguments');
}
}

// More on proxying: https://www.chromium.org/developers/design-documents/network-settings
if (options.proxyServer) {
args.push(`--proxy-server=${options.proxyServer}`);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "n8n-nodes-puppeteer",
"version": "1.1.2",
"version": "1.2.0",
"description": "n8n node for browser automation using Puppeteer",
"license": "MIT",
"homepage": "https://github.com/drudge/n8n-nodes-puppeteer",
Expand Down

0 comments on commit 1ad9b3b

Please sign in to comment.