Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Could not find Chrome (ver. ${this.puppeteer.browserRevision}) on Docker #12332

Closed
1 of 2 tasks
vickonrails opened this issue Apr 25, 2024 · 7 comments
Closed
1 of 2 tasks

Comments

@vickonrails
Copy link

vickonrails commented Apr 25, 2024

Minimal, reproducible example

// rest of code 
const browser = await puppeteer.launch({
            headless: true,
            args: ['--no-sandbox']
        })
  const page = await browser.newPage()
  await page.setContent(html)
  const pdf = await page.pdf({ format: 'A4', printBackground: true })
  await browser.close();
  return pdf

// rest of code

Error string

Could not find Chrome (ver. ${this.puppeteer.browserRevision})

Bug behavior

  • Flaky
  • PDF

Background

An API in my Node project exposes a route that accepts HTML in the body. This markup is used alongside some CSS to generate a PDF and send a buffer back as a response.

NB: I was previous using Bun, but I switched to node to make sure it wasn't a problem with bun.

Here's my docker file

FROM node

# Set working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy rest of the application code
COPY . .

# Set environment variable for production
ENV NODE_ENV production

# Specify command to start the application
CMD ["node", "index.js"]

I made a repro here

Expectation

The endpoint returns a PDF when running in a docker container as it works when running normally as a node app.

Reality

This works when I run as a node app, but when I try to run in a docker container, it gives the following error

Could not find Chrome (ver. 124.0.6367.60). This can occur if either

  1. you did not perform an installation before running the script (e.g. npx puppeteer browsers install chrome) or
  2. your cache path is incorrectly configured (which is: /usr/src/app/.cache/puppeteer).\nFor (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration

Puppeteer configuration file (if used)

const { join } = require('path');

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
    // Changes the cache location for Puppeteer.
    cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};

Puppeteer version

"puppeteer": "^22.7.1"

Node version

"node":"^21.1.0"

Package manager

yarn

Package manager version

1.22.19

Operating system

macOS

Copy link

github-actions bot commented Apr 25, 2024

This issue has an invalid Node.js version: "node":"^21.1.0". Versions must follow SemVer formatting. Please update the form with a valid version.


Analyzer run

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 25, 2024

you need to run installation after your copied the config into the project folder

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 25, 2024

So probably

FROM node

# Set working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
# Copy rest of the application code
COPY . .
RUN npm install

# Set environment variable for production
ENV NODE_ENV production

# Specify command to start the application
CMD ["node", "index.js"]

would work.

@OrKoN OrKoN closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2024
@vickonrails
Copy link
Author

Thank you so much for looking into this @OrKoN. Unfortunately, moving the install after the copy command didn't change anything.

Generally, I think it has to do with the way the chrome browser is resolved in the docker environment (vs my normal running application). Here are a couple of reasons I think so

  • .cache is present in both docker and normal install (I can even dig deeper to find the Google Chrome For Test)
  • It currently points to /usr/src/app/.cache/puppeteer which is where puppeteer installs chrome.

But I still can't figure out why it would find it in a docker environment

@github-actions github-actions bot added invalid and removed invalid labels Apr 25, 2024
@OrKoN
Copy link
Collaborator

OrKoN commented Apr 25, 2024

@vickonrails perhaps you need to make sure your local node_modules do not get copied to the Dockerfile cause in that case npm install might not install Puppeteer.

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 25, 2024

FROM node

# Set working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./

# Copy rest of the application code
COPY index.js index.js

RUN ls

RUN npm install

# Set environment variable for production
ENV NODE_ENV production

# Specify command to start the application
CMD ["node", "index.js"]

finds the binary. Probably, after that it will fail to launch but to solve this see the official dockerfile we provide https://github.com/puppeteer/puppeteer/blob/main/docker/Dockerfile which has Puppeteer and the browser pre-installed.

@vickonrails
Copy link
Author

If anyone runs into this, I solved this by installing chrome manually.

Here's what my Dockerfile looks like

# I'm using bun but you can replace with node
FROM oven/bun

# manually download chromium and some fonts. It's important to know the package manager your base image uses. 
# In this case `oven/bun` is based on debian and apt-get is the package manager

RUN apt-get update && apt-get install -y \
    fonts-dejavu fonts-droid-fallback fonts-freefont-ttf fonts-liberation \
    chromium \
    && rm -rf /var/lib/apt/lists/*

# set the executable path and tell puppeteer to not download chromium
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

WORKDIR /usr/src/app

COPY package*.json ./
COPY . .

# Install after copying the files
RUN bun install

ENV NODE_ENV production

CMD [ "bun", "start" ]

I hope this helps someone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants