Code Monkey home page Code Monkey logo

Comments (6)

mt-u avatar mt-u commented on May 13, 2024 4

Hi @lucacasonato, @halvardssm.

I solved this.
I checked the error message by adding console.log (line); between lines 167 and 168 of BrowserRunner.ts in deno-puppeteer.
I found that the cause of the error was that some required packages were not installed.
The packages listed in troubleshooting of puppeteer are not enough for Docker containers, need the following:

libdrm2
libxkbcommon0
libxshmfence1

And one more important thing is written here.
If we run as root user inside a Docker container, we need to use --no-sandbox mode when launching Puppeteer.
Otherwise error will occur.
Yet one more thing, Troubleshooting has tips on shared memory issues in Docker containers.
Based on these, the parameters of the launch method should be as follows:

const browser = await puppeteer.launch({
  args: [
    '--no-sandbox',
    '--disable-dev-shm-usage'
  ],
});

This is my Dockerfile:

FROM debian:buster-slim

ENV DENO_VERSION=1.10.1
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get -qq update \
    && apt-get -qq install -y --no-install-recommends \
    curl \
    ca-certificates \
    unzip \
# ↓ https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# Since I want to leave the contents of troubleshooting.md as it is, ca-certificates is intentionally duplicated here.
    ca-certificates \
    fonts-liberation \
    libappindicator3-1 \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgbm1 \
    libgcc1 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    lsb-release \
    wget \
    xdg-utils \
# ↑ https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# ↓ Added based on the information obtained from by console.log(line) at https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts#L168.
    libdrm2 \
    libxkbcommon0 \
    libxshmfence1 \
# ↑ Added based on the information obtained from by console.log(line) at https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts#L168.
    && curl -fsSL https://github.com/denoland/deno/releases/download/v${DENO_VERSION}/deno-x86_64-unknown-linux-gnu.zip \
    --output deno.zip \
    && unzip deno.zip \
    && rm deno.zip \
    && chmod 755 deno \
    && mv deno /usr/bin/deno \
    && apt-get -qq remove --purge -y \
    curl \
# Do not remove ca-certificates as it is required by puppeteer.
#    ca-certificates \
    unzip \
    && apt-get -y -qq autoremove \
    && apt-get -qq clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN useradd --uid 1993 --user-group deno \
 && mkdir /deno-dir/ \
 && chown deno:deno /deno-dir/

ENV DENO_DIR /deno-dir/

# https://deno.land/x/[email protected]#installation
RUN PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/[email protected]/install.ts

WORKDIR /root
ENTRYPOINT ["deno"]
CMD ["run", "https://deno.land/std/examples/welcome.ts"]

Thank you.

from deno-puppeteer.

lucacasonato avatar lucacasonato commented on May 13, 2024 3

Added an example Dockerfile and info to the README. Thanks everyone for figuring this out. Especially @mt-u who got it working in the end :-)

from deno-puppeteer.

bennettdams avatar bennettdams commented on May 13, 2024 1

Same goes for running with WSL 2.

from deno-puppeteer.

jupegarnica avatar jupegarnica commented on May 13, 2024

same here

from deno-puppeteer.

jsejcksn avatar jsejcksn commented on May 13, 2024

Source:

async function waitForWSEndpoint(
browserProcess: Deno.Process,
timeout: number,
preferredRevision: string
): Promise<string> {
const timeId = setTimeout(() => {
throw new TimeoutError(
`Timed out after ${timeout} ms while trying to connect to the browser! Only Chrome at revision r${preferredRevision} is guaranteed to work.`
);
}, timeout);
for await (const line of readLines(browserProcess.stderr!)) {
const match = line.match(/^DevTools listening on (ws:\/\/.*)$/);
if (match) {
clearTimeout(timeId);
return match[1];
}
}
clearTimeout(timeId);
throw new Error(
[
"Failed to launch the browser process!" + "",
"TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md",
"",
].join("\n")
);
}

Haven't tried to debug this yet, but here are some initial thoughts:

  1. Is the "listening" message still being emitted to stderr and not stdout?
  2. Is the regex still accurate?

from deno-puppeteer.

mt-u avatar mt-u commented on May 13, 2024

Hi @lucacasonato .
When I run deno and puppeteer on a Docker container, I get the same error message as @halvardssm.
So I recreated the Dockerfile according to the trouble shooting link shown in the error message.
Then I got another error.
error: Uncaught (in promise) BadResource: Bad resource ID
Does this help anything?

Dockerfile is

FROM debian:buster-20210511-slim

ENV DENO_VERSION=1.10.1
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get -qq update \
    && apt-get -qq install -y --no-install-recommends \
    curl \
    ca-certificates \
    unzip \
# ↓ from puppeteer instruction https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-in-docker
    wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && 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 -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
      --no-install-recommends \
# ↑ from puppeteer instruction https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-in-docker
    && curl -fsSL https://github.com/denoland/deno/releases/download/v${DENO_VERSION}/deno-x86_64-unknown-linux-gnu.zip \
    --output deno.zip \
    && unzip deno.zip \
    && rm deno.zip \
    && chmod 755 deno \
    && mv deno /usr/bin/deno \
    && apt-get -qq remove --purge -y \
    curl \
    ca-certificates \
    unzip \
    && apt-get -y -qq autoremove \
    && apt-get -qq clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN useradd --uid 1993 --user-group deno \
 && mkdir /deno-dir/ \
 && chown deno:deno /deno-dir/

ENV DENO_DIR /deno-dir/

# https://deno.land/x/[email protected]#installation
RUN PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/[email protected]/install.ts

WORKDIR /root
ENTRYPOINT ["deno"]
CMD ["run", "https://deno.land/std/examples/welcome.ts"]

Script file(sample.ts) is

import puppeteer from "https://deno.land/x/[email protected]/mod.ts";

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://sample.com");
await page.screenshot({ path: '/root/sample.png' });
await browser.close();

Error message is

$ docker build -t deno .

... building docker image ...
... after successfully built ...

$ docker run --rm -it -v path-to-sample-script-dir:/root deno:latest run -A --unstable sample.ts
Check file:///root/sample.ts
error: Uncaught (in promise) BadResource: Bad resource ID
    const result = await reader.read(inspectArr);
                   ^
    at deno:core/core.js:86:46
    at unwrapOpResult (deno:core/core.js:106:13)
    at async read (deno:runtime/js/12_io.js:97:19)
    at async readDelim (https://deno.land/[email protected]/io/bufio.ts:652:20)
    at async readStringDelim (https://deno.land/[email protected]/io/bufio.ts:702:20)
    at async readLines (https://deno.land/[email protected]/io/bufio.ts:711:18)
    at async waitForWSEndpoint (https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts:167:20)
    at async BrowserRunner.setupConnection (https://deno.land/x/[email protected]/src/deno/BrowserRunner.ts:145:31)
    at async ChromeLauncher.launch (https://deno.land/x/[email protected]/src/deno/Launcher.ts:114:26)
    at async file:///root/sample.ts:4:17

from deno-puppeteer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.