如何在 MacOS Apple Silicon M1 arm64 主机上的 Docker 容器上运行 Puppeteer

Jos*_*abo 6 node.js docker jestjs puppeteer

我无法在 Docker 容器内的 Puppeteer 上运行单元测试。我的主机是带有Apple Silicon M1(arm64)芯片的MacOS。我还尝试遵循Puppeteer Github 文档中的说明,但它们适用于 amd64。有什么建议吗?

Jos*_*abo 15

由于某种原因直接安装 puppeteer NPM 包不起作用,遗憾的是 GitHub 中的官方 Puppeteer 文档与arm64架构不兼容。

这就是我准备 Dockerfile 的方式:

FROM node:16

RUN apt-get update \
 && apt-get install -y chromium \
    fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
    --no-install-recommends
    
USER node # non-root user that comes with `node` images.

WORKDIR /app

COPY --chown=node package.json .
COPY --chown=node package-lock.json .

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium

RUN npm install

COPY --chown=node . /app
Run Code Online (Sandbox Code Playgroud)

配置的 Dockerfile 原生于 Apple Silicon (linux/arm64) 以及原生于 amd64 (linux/amd64) 架构。

您还必须--no-sandbox在代码中将参数传递给浏览器:

FROM node:16

RUN apt-get update \
 && apt-get install -y chromium \
    fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
    --no-install-recommends
    
USER node # non-root user that comes with `node` images.

WORKDIR /app

COPY --chown=node package.json .
COPY --chown=node package-lock.json .

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium

RUN npm install

COPY --chown=node . /app
Run Code Online (Sandbox Code Playgroud)

额外的标志会禁用浏览器的开发沙箱,因此请确保仅访问受信任的页面。否则就好了。请来自未来的人,请随意编辑此答案或评论如何摆脱争论--no-sandbox


Ste*_*gin 6

我能够Puppeteer通过Dockeron运行的唯一方法Apple Silicon是 by。

  1. amd64通过指定平台创建镜像
FROM --platform=linux/amd64 node:18
Run Code Online (Sandbox Code Playgroud)
  1. 在 Docker Desktop 中启用“使用 Rosetta”支持

在此输入图像描述

  1. 按照当前Puppeteer Docker说明手动安装,Chromium而不是将其安装为 Puppeteer 的一部分。

修改后的 Puppeteer Docker 文件

此示例简化了当前的 Puppeteer 指令。我删除了他们的创建用户说明,因为图像中包含了这一说明node。此外,它们会在安装 Chromium 后清除apt registry,阻止您安装其他任何东西。如果您觉得有必要,请将其添加回来。


FROM --platform=linux/amd64 node:18

# We can define environment variables here
# instead of specifying them when we launch Puppeteer. 
# The path may change depending on your platform and installed binary.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable


RUN apt-get update \
    && apt-get install -y wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg \
    && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && 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 \
      --no-install-recommends

# If you desire to run in Chrome sandbox mode, change to a non-root user, and make sure you launch your image with `cap_add: SYS_ADMIN`.

USER node
Run Code Online (Sandbox Code Playgroud)

启用 Chromium Sandbox 以非 root 身份运行

  1. 在你的系统中设置一个非root用户dockerfile
  2. 启动映像时启用 cap_add: SYS_ADMIN。

例子docker compose

services: {
  node: {
    cap_add:
      - SYS_ADMIN
  }
}
Run Code Online (Sandbox Code Playgroud)

通过在 中设置环境变量DOCKERFILE、设置 cap_add 并运行非 root 用户,您可以使用默认的偏执沙箱来启动 Puppeteer。

在 Dockerfile 中

# Use the non-root user that comes with `node` image,
# Or another if you wish.
USER node
Run Code Online (Sandbox Code Playgroud)

在你的 JavaScript 中

FROM --platform=linux/amd64 node:18
Run Code Online (Sandbox Code Playgroud)

以 root 身份运行并绕过 Chromium 沙箱

如果您宁愿粗暴地禁用 Chromium Sandbox,只需以默认root用户身份运行,然后关闭沙箱保护即可。

  1. 不要在您的中设置用户dockerfile
  2. 启动 Puppeteer 时,禁用沙箱

FROM --platform=linux/amd64 node:18

# We can define environment variables here
# instead of specifying them when we launch Puppeteer. 
# The path may change depending on your platform and installed binary.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable


RUN apt-get update \
    && apt-get install -y wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg \
    && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && 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 \
      --no-install-recommends

# If you desire to run in Chrome sandbox mode, change to a non-root user, and make sure you launch your image with `cap_add: SYS_ADMIN`.

USER node
Run Code Online (Sandbox Code Playgroud)

参考:

https://github.com/puppeteer/puppeteer/blob/main/docker/Dockerfile