【问题标题】:Puppeter with Firefox works locally on Windows, but doesnt work on docker container带有 Firefox 的 Puppeteer 在 Windows 上本地工作,但在 docker 容器上不起作用
【发布时间】:2022-03-21 03:45:42
【问题描述】:

我有一个项目可以在 Windows 上本地运行 nodejs 没有问题。 firefox 会自动从 node_modules/puppeteer/.local-firefox 中的 npm install puppeteer@latest 下载

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch({
        product: 'firefox',
        headless: true,
        defaultViewport: {
            width: 3544,
            height: 3544,
        },
    });

问题是当我尝试在 docker 上做同样的事情时。

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch({
      product: 'firefox',
      headless: true,
      defaultViewport: {
        width: pageSizePx.width,
        height: pageSizePx.height,
      },
    });

这就是 docker linux 上的结果。

安装正常,firefox在同一个地方。 node_modules/puppeteer/.local-firefox

Docker File

# base image
FROM node:12.15-alpine

# ENV CHROME_BIN="/usr/bin/chromium-browser"\
#     PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"
########## 
## Update and Install packages
##########
RUN set -x \
    && apk update \
    && apk upgrade \
    && echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" > /etc/apk/repositories \
    && echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
    && echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories \
    # add the packages
    ## g++: used to install NodeJS related packages
    ## chromium: used to run Puppeteer
    && apk add --no-cache g++ firefox 

WORKDIR /scripts
WORKDIR /app

RUN apk add --no-cache curl bash
#RUN npm install -g ts

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

【问题讨论】:

  • 请显示你的 Dockerfile
  • @DevGuyAhnaf 我用 docker 文件编辑了这个问题。

标签: node.js puppeteer


【解决方案1】:

为了使用firefox,我使用了另一个类似的puppeter选项,库是playwright。

https://playwright.dev/docs/docker

【讨论】:

    猜你喜欢
    • 2020-12-24
    • 2021-01-29
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2017-08-31
    相关资源
    最近更新 更多