You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中运行Node.js+Puppeteer应用启动失败求助

问题描述

我尝试在Docker镜像中运行一个集成Puppeteer的Node.js应用,镜像构建和运行正常,应用其他功能也能正常工作,但执行Puppeteer代码时抛出错误。以下是相关配置和错误信息:

Dockerfile

FROM node:slim

# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
    wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
    sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
    apt-get update && \
    apt-get install google-chrome-stable -y --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

# FROM public.ecr.aws/lambda/nodejs:14.2022.09.09.11
# Create working directory
WORKDIR /usr/src/app

# Copy package.json
COPY package*.json ./

# Install NPM dependencies for function
RUN npm install

COPY . .

RUN npm run build

# Expose app
EXPOSE 6060

# Run app
CMD ["npm","start"]

Puppeteer启动代码

const browser = await puppeteer.launch({
  headless: "new",
  executablePath: "/usr/bin/google-chrome",
  args: ["--no-sandbox", "--disable-gpu"],
});

运行命令

docker build --platform linux/amd64 -t puppeteer-backend .
docker run --rm -p 6060:6060 puppeteer-backend

错误信息

Error: Failed to launch the browser process!
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
[77:77:0621/204259.813010:ERROR:nacl_fork_delegate_linux.cc(313)] Bad NaCl helper startup ack (0 bytes)
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
[39:86:0621/204259.935395:ERROR:file_path_watcher_inotify.cc(315)] inotify_init() failed: Function not implemented (38)
[39:89:0621/204259.960811:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0621/204300.029954:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)
Assertion failed: p_rcu_reader->depth != 0 (/qemu/include/qemu/rcu.h: rcu_read_unlock: 101)


TROUBLESHOOTING: https://pptr.dev/troubleshooting

    at ChildProcess.onClose (/usr/src/app/node_modules/@puppeteer/browsers/src/launch.ts:398:11)
    at ChildProcess.emit (node:events:523:35)
    at ChildProcess.emit (node:domain:489:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12)

解决方案

从错误信息来看,核心问题是跨架构模拟(qemu)不兼容Chrome运行,同时缺少Chrome必要的系统依赖和配置。可以从以下几个方向修复:

1. 调整基础镜像与依赖安装

替换node:slim为更稳定的node:18-bookworm-slim,并补充Chrome运行所需的完整依赖:

FROM node:18-bookworm-slim

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable

# 安装Chrome及配套依赖
RUN apt-get update && apt-get install -y \
    google-chrome-stable \
    fonts-ipafont-gothic \
    fonts-wqy-zenhei \
    fonts-thai-tlwg \
    fonts-khmeros \
    fonts-kacst \
    fonts-freefont-ttf \
    libxss1 \
    dbus-x11 \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 6060
CMD ["npm","start"]

2. 优化Puppeteer启动参数

添加适配Docker环境的参数,规避权限和资源限制:

const browser = await puppeteer.launch({
  headless: "new",
  executablePath: "/usr/bin/google-chrome-stable",
  args: [
    "--no-sandbox",
    "--disable-gpu",
    "--disable-dev-shm-usage",
    "--disable-setuid-sandbox",
    "--no-zygote",
    "--single-process",
    "--disable-features=VizDisplayCompositor"
  ],
  ignoreDefaultArgs: ["--disable-extensions"]
});

3. 解决跨架构模拟问题

如果是在ARM架构机器(如M1/M2 Mac)上构建amd64镜像:

  • 开启Docker的Rosetta模拟:进入Docker设置 -> Features in development,勾选"Use Rosetta for x86/amd64 emulation on Apple Silicon"
  • 或者移除构建命令中的--platform linux/amd64参数,直接构建对应本机架构的镜像

4. 修复dbus连接问题

错误中提到无法连接dbus,可在Dockerfile中添加初始化步骤:

# 在CMD前添加
RUN mkdir -p /run/dbus
RUN dbus-daemon --system --fork

或者运行容器时挂载主机的dbus socket(仅测试环境使用):

docker run --rm -p 6060:6060 -v /run/dbus/system_bus_socket:/run/dbus/system_bus_socket puppeteer-backend

内容的提问来源于stack exchange,提问作者Anas Latique

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 05:45:02