我在构建后端 docker(特别是安装 Puppeteer)时遇到错误。我使用的是 M1 MacBook,我在本地机器上找到了解决方案(https://github.com/puppeteer/puppeteer/issues/6622),但这在 docker 上不起作用。有谁在 docker 上遇到过同样的 Puppeteer 问题吗?
#12 15.58 npm ERR! code 1
#12 15.58 npm ERR! path /app/node_modules/puppeteer
#12 15.58 npm ERR! command failed
#12 15.58 npm ERR! command sh -c node install.js
#12 15.58 npm ERR! The chromium binary is not available for arm64.
#12 15.58 npm ERR! If you are on Ubuntu, you can install with:
#12 15.58 npm ERR!
#12 15.58 npm ERR! sudo apt install chromium
#12 15.58 npm ERR!
#12 15.58 npm ERR!
#12 15.58 npm ERR! sudo apt install chromium-browser
#12 15.58 npm ERR!
#12 15.58 npm ERR! /app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js:115
#12 15.58 npm ERR! throw new Error();
FROM --platform=linux/amd64 node:16-alpine
WORKDIR /app
EXPOSE 8000
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV DOCKER_DEFAULT_PLATFORM "linux/amd64"
COPY . .
RUN apk --no-cache add --virtual builds-deps build-base python3 && \
npm install
CMD ["npm", "start"]
这是对我有用的 dockerfile。
# reference https://developers.google.com/web/tools/puppeteer/troubleshooting#setting_up_chrome_linux_sandbox
FROM node:current-alpine
# manually installing chrome
RUN apk add chromium
# skips puppeteer installing chrome and points to correct binary
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm ci
# the rest of your dockerfile here
在 js 中启动 puppeteer 时,请确保在启动时添加以下标志
puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
您可以在 google 文档
中阅读更多相关信息我有同样的错误,这使它对我有用:https://github.com/puppeteer/puppeteer/issues/7740#issuecomment-970490323
npm 安装似乎找不到 M1 的 chromium 二进制文件。为了不让 npm 尝试安装 Chromium,您可以添加
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
。但随后我还需要在运行 npm install
之前安装二进制文件,方法是将 RUN apk add chromium
添加到 Dockerfile(如果您正在运行节点 alpine 映像)。
结果:
FROM node:16-alpine
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
RUN apk add chromium
# Add and install everything
对于任何人:
Some index files failed to download. They have been ignored, or old ones used instead.
您需要将存档链接添加到容器的
sources.list
,如下所示:
# Patch sources list for chromium and other packages that have been archived
RUN echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list
这是一个工作 dockerfile,用于在基于 ARM 的 docker 容器上运行 puppeteer。
FROM --platform=linux/arm64 ubuntu:22.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get install -y gconf-service apt-transport-https ca-certificates libssl-dev wget libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils curl build-essential tar gzip findutils net-tools dnsutils telnet ngrep tcpdump
RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:saiarcot895/chromium-dev
RUN apt update
RUN apt-get install -y chromium-browser
ENV NODE_VERSION 14.17.0
RUN ARCH=arm64 \
&& curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
&& tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
&& rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs \
# smoke tests
&& node --version \
&& npm --version
RUN ARCH=arm64 \
&& npm install -g [email protected]
ENTRYPOINT ["/bin/sh", "-c", "bash"]
构建依赖关系后添加 puppeteer env。为我工作。
FROM node:16-alpine3.11
WORKDIR /usr/app
COPY package*.json ./
COPY tsconfig.json ./
RUN apk --no-cache --virtual build-dependencies add \
python3 \
make \
g++
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
RUN npm install --quiet
RUN npm install -g pm2 --quiet
COPY ./ ./
RUN npm run build
RUN rm -rf ./src