Heroku上使用Puppeteer启动Chrome失败的问题求助
Heroku中Puppeteer启动失败:「No usable sandbox!」问题排查与解决
问题描述
在Heroku环境使用Puppeteer时,浏览器进程启动失败,错误提示为「No usable sandbox!」。已尝试以下操作但未解决:
- 代码中添加
--no-sandbox和--disable-setuid-sandbox启动参数 - 安装puppeteer heroku buildpack
错误日志
2022-10-03T16:51:52.616196+00:00 app[web.1]: Error: Failed to launch the browser process! 2022-10-03T16:51:52.616214+00:00 app[web.1]: [1003/165152.260124:FATAL:zygote_host_impl_linux.cc(117)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox. 2022-10-03T16:51:52.616214+00:00 app[web.1]: #0 0x55894fa52339 base::debug::CollectStackTrace() 2022-10-03T16:51:52.616215+00:00 app[web.1]: #1 0x55894f9c8f23 base::debug::StackTrace::StackTrace() 2022-10-03T16:51:52.616215+00:00 app[web.1]: #2 0x55894f9c6070 logging::LogMessage::~LogMessage() 2022-10-03T16:51:52.616215+00:00 app[web.1]: #3 0x55894d9c6c2b content::ZygoteHostImpl::Init() 2022-10-03T16:51:52.616216+00:00 app[web.1]: #4 0x55894f543c0f content::ContentMainRunnerImpl::Initialize() 2022-10-03T16:51:52.616216+00:00 app[web.1]: #5 0x55894f541bfd content::RunContentProcess() 2022-10-03T16:51:52.616216+00:00 app[web.1]: #6 0x55894f541d4e content::ContentMain() 2022-10-03T16:51:52.616217+00:00 app[web.1]: #7 0x55894f59920a headless::(anonymous namespace)::RunContentMain() 2022-10-03T16:51:52.616218+00:00 app[web.1]: #8 0x55894f598f15 headless::HeadlessShellMain() 2022-10-03T16:51:52.616219+00:00 app[web.1]: #9 0x55894be7a1e3 ChromeMain 2022-10-03T16:51:52.616219+00:00 app[web.1]: #10 0x7f8e87025d90 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) 2022-10-03T16:51:52.616219+00:00 app[web.1]: #11 0x7f8e87025e40 __libc_start_main 2022-10-03T16:51:52.616220+00:00 app[web.1]: #12 0x55894be7a02a _start 2022-10-03T16:51:52.616221+00:00 app[web.1]: 2022-10-03T16:51:52.616222+00:00 app[web.1]: 2022-10-03T16:51:52.616222+00:00 app[web.1]: 2022-10-03T16:51:52.616222+00:00 app[web.1]: TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md 2022-10-03T16:51:52.616223+00:00 app[web.1]: 2022-10-03T16:51:52.616223+00:00 app[web.1]: at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:286:20) 2022-10-03T16:51:52.616224+00:00 app[web.1]: at Interface.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:274:24) 2022-10-03T16:51:52.616224+00:00 app[web.1]: at Interface.emit (node:events:525:35) 2022-10-03T16:51:52.616225+00:00 app[web.1]: at Interface.close (node:readline:590:8) 2022-10-03T16:51:52.616225+00:00 app[web.1]: at Socket.onend (node:readline:280:10) 2022-10-03T16:51:52.616225+00:00 app[web.1]: at Socket.emit (node:events:525:35) 2022-10-03T16:51:52.616226+00:00 app[web.1]: at endReadableNT (node:internal/streams/readable:1358:12) 2022-10-03T16:51:52.616226+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21)
当前代码
const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"], });
解决方案
1. 调整Buildpack顺序
将puppeteer的buildpack放在node.js buildpack之前,确保浏览器依赖先于项目代码安装。同时使用官方维护的puppeteer buildpack版本,避免兼容性问题。
2. 补充启动参数
在现有参数基础上添加适配Heroku容器环境的参数:
const browser = await puppeteer.launch({ args: [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage", "--disable-gpu", "--single-process" ], headless: "new" // Puppeteer v19+推荐使用新无头模式 });
--disable-dev-shm-usage:解决Heroku容器中共享内存空间不足的问题--disable-gpu:禁用GPU加速,适配无图形环境--single-process:强制单进程运行,规避沙箱进程隔离冲突
3. 设置环境变量
在Heroku应用设置中添加环境变量PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true,让Puppeteer使用buildpack安装的Chromium,避免自带版本与环境不兼容。
4. 更新Heroku栈版本
切换到最新的Heroku栈(如heroku-22),旧栈的内核或系统库可能无法适配Chromium的运行要求,即使禁用沙箱也会报错。
5. 指定Chromium路径
如果以上方法无效,手动指定buildpack安装的Chromium路径:
const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"], executablePath: process.env.CHROME_BIN || "/app/.apt/usr/bin/google-chrome" });
内容的提问来源于stack exchange,提问作者thlord
相关产品推荐
相关产品推荐

