Troubleshooting Puppeteer: Chrome Not Found in Docker Environment

I’m having trouble with Puppeteer in a Docker setup. I’m trying to create PDFs and also use Selenium for some automation stuff. Here’s what I’ve got in my Dockerfile:

FROM node:16-alpine
WORKDIR /app
RUN apk update && apk add --no-cache \
    firefox \
    firefox-geckodriver \
    bash \
    curl \
    tar \
    gzip \
    gcc \
    && npm install -g webdriverio

ENV FIREFOX_BIN=/usr/bin/firefox
ENV GECKODRIVER_PATH=/usr/bin/geckodriver
ENV PUPPETEER_SKIP_DOWNLOAD=true

COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 8080

CMD ["node", "app.js"]

My PDF generation code looks like this:

const browser = await puppeteer.launch({
  headless: "new",
  args: ['--no-sandbox'],
});
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.emulateMediaType('screen');
await page.waitForTimeout(1500);
const pdf = await page.pdf({
  path: 'output.pdf',
  format: 'A4',
  printBackground: true,
});
await browser.close();

When I run this on my EC2 instance, I get an error saying it can’t find Chrome. Any ideas on how to fix this? I’m not sure if it’s a setup issue or something else.

hey, i ran into this issue too. try adding chromium to ur docker image like this:

RUN apk add --no-cache chromium
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

then update ur puppeteer launch options to use the executable path. that should fix it. good luck!

The issue you’re encountering is likely due to Puppeteer not finding a compatible Chrome installation in your Docker environment. Your Dockerfile is set up for Firefox and Selenium, but Puppeteer requires Chrome.

To resolve this, you’ll need to install Chromium in your Docker image. Modify your Dockerfile to include:

RUN apk add --no-cache chromium
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

Then, update your Puppeteer launch options:

const browser = await puppeteer.launch({
  executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
  headless: 'new',
  args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

This should allow Puppeteer to find and use Chromium in your Docker container. Remember to rebuild your Docker image after making these changes.

I’ve dealt with similar Puppeteer issues in Docker before. One thing to note is that Alpine-based images can be tricky with Puppeteer. Instead of struggling with Alpine, I’d recommend switching to a Debian-based image like node:16-slim. It’s still lightweight but more compatible with Puppeteer out of the box.

In your Dockerfile, you’d then use:

RUN apt-get update && apt-get install -y chromium

And set the environment variable:

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

This approach has worked well for me in production. It avoids some of the headaches that come with Alpine while still keeping the image relatively small. Just remember to adjust your package manager commands from apk to apt-get when you make the switch.