Puppeteer无法找到Chrome,Alpine版Linux如何安装chrome
Author:zhoulujun Date:
我希望用Puppeteer来把网页截图为pdf存档,但是
在本地调试Puppeteer毫无问题,但是在docker里面,就无法成功,报错!
/root/.cache/puppeteer/chrome/linux-131.0.6778.204/chrome-linux64/chrome ENOENT\n\n\nTROUBLESHOOTING: https://pptr.dev/troubleshooting"
npx安装chrome
但是 npx install chrome 不是安装上了
npx puppeteer browsers install chrome # [email protected] /root/.cache/puppeteer/chrome/linux-132.0.6834.83/chrome-linux64/chrome
为什么找不到呢?查看文件,chrome是存在的
/data # ls /root/.cache/puppeteer/chrome/linux-132.0.6834.83/chrome-linux64/ ABOUT chrome_sandbox product_logo_48.png MEIPreload deb.deps resources PrivacySandboxAttestationsPreloaded hyphen-data resources.pak WidevineCdm icudtl.dat rpm.deps chrome libEGL.so v8_context_snapshot.bin chrome-wrapper libGLESv2.so vk_swiftshader_icd.json chrome_100_percent.pak libvk_swiftshader.so xdg-mime chrome_200_percent.pak libvulkan.so.1 xdg-settings chrome_crashpad_handler locales /data #
但是,无论 which chromium 还是 which google-chrome 确实找不到
而且环境变量 无论是echo $PUPPETEER_SKIP_DOWNLOAD 与 env | grep PUPPETEER_SKIP_DOWNLOAD
都没意设置 跳过 chrome 安装!
apk安装chrome
npx无法安装,只有靠apk安装,但是linux 无论centos、debian、alpine都缺乏必要的依赖,比如
apt-get update && apt-get install -y \ wget \ ca-certificates \ fonts-liberation \ libappindicator3-1 \ libasound2 \ libatk1.0-0 \ libatk-bridge2.0-0 \ libcups2 \ libdbus-1-3 \ libnss3 \ libxss1 \ libxtst6 \ libx11-xcb1 \ libxcomposite1 \ libxrandr2 \ libxdamage1 \ libxcursor1 \ libwayland-client0 \ libwayland-cursor0 \ libwayland-egl1 \ libgdk-pixbuf2.0-0 \ --no-install-recommends && \ rm -rf /var/lib/apt/lists/*
但在alpine,只需
apk add --no-cache chromium nss freetype harfbuzz ca-certificates ttf-freefont
最终镜像为
# Base image FROM zenika/alpine-chrome:with-node # FROM node:20-alpine # Create app directory WORKDIR /app # Install app dependencies COPY package.json /app/ # Install necessary packages for Puppeteer RUN apk add --no-cache chromium nss freetype harfbuzz ca-certificates ttf-freefont #RUN npx puppeteer browsers install chrome ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \ UPPETEER_SKIP_CHROMIUM_DOWNLOAD="true" RUN yarn install --ignore-scripts && yarn cache clean # Bundle app source COPY . /app # EXPOSE 8000 CMD [ "yarn", "start" ]
这样就好了
在Puppeteer,需要设置
export async function createPDF(pageUrl, host, cookies) { const executablePath = process.env.PUPPETEER_EXECUTABLE_PATH const browser = await puppeteer.launch({ ignoreHTTPSErrors: true, ...(executablePath ? { executablePath } : {}), args: [ '--no-sandbox', '--disable-web-security', '--disable-setuid-sandbox', '--ignore-certificate-errors', '--disable-gpu' /* // 远程调试端口 '--remote-debugging-port=18523', '--remote-debugging-address=0.0.0.0' */ ], slowMo: 250 // devtools: true, // 自动开启 F12 // headless: false // 开启界面 }) }
这样,就搞定了
当然,最简单的办法就是 直接用 大有chrome的镜像,比如 zenika/alpine-chrome:with-node
参考文章:
https://blog.uuphy.com/posts/记录一次-puppeteer-在-docker-容器内无法启动的问题/
转载本站文章《Puppeteer无法找到Chrome,Alpine版Linux如何安装chrome》,
请注明出处:https://www.zhoulujun.cn/html/webfront/browser/Puppeteer/9439.html