问题
When I run Puppeteer on Cloud Functions with Node 8, I get this error.
Error: Failed to launch chrome!
[1205/063825.588245:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
Launching Puppeteer with
browser = await puppeteer.launch({
args: ['--no-sandbox'],
headless: true
})
solves the issue. I have searched on the internet and could not find any reason why is this necessary?
Why does Headless Chrome run as root in the first place?
回答1:
As Cloud Functions is serverless you can do not much more than using this Chromium flag at launch each time.
Background
The usage of --no-sandbox
flag is only a workaround to make browser launch possible on Linux systems.
It is related to security, concretely: Linux sandboxing and it is advised to set up your own sandbox for Chromium if you are dealing with untrusted web traffic instead of using chrome without one.
You can set up a sendbox on Linux with user namespace cloning. In case of Cloud Functions I am not aware of any solutions if this could be set up: so you need the no-sandbox
, but make sure your puppeteer scripts visiting only trusted pages.
来源:https://stackoverflow.com/questions/53681161/why-puppeteer-needs-no-sandbox-to-launch-chrome-in-cloud-functions