Benjamin
Benjamin

Reputation: 721

Install a Firefox browser binary for Puppeteer in Docker

I'm trying to run a script with a dependency of https://github.com/pevers/images-scraper in a Docker container. I can run the script on its own (on my machine which has Node 14.15.4, doing node src/index.js which will yield the console outout of A), but in the context of docker it is not working - I am getting an error message about a missing Firefox binary.

Dockerfile:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

RUN PUPPETEER_PRODUCT=firefox npm install puppeteer

COPY . .

CMD [ "node", "src/index.js" ]

src/index.js:

let Scraper = require("images-scraper");

const google = new Scraper({
  puppeteer: {
    userAgent:
      "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0", 
    headless: true,
    safe: true,
  },
});


(async () => {
  const A = await google.scrape("bananas", 200);
  console.log(A)
})();

package.json

{
    "name": "test",
    "version": "1.0.0",
    "scripts": {
      "test": "node src/index.js"
    },
    "dependencies": {
      "images-scraper": "^6.2.1"
    }
  }

When I /bin/bash into my container and do node src/index.js I get the error:

    at ChromeLauncher.launch (/usr/src/app/node_modules/puppeteer/lib/cjs/puppeteer/node/Launcher.js:79:23)
    at async GoogleScraper.scrape (/usr/src/app/node_modules/images-scraper/src/google/scraper.js:53:21)
    at async /usr/src/app/src/index.js:19:13

New to using Node with Docker - I was wondering where I am going wrong here. Thanks

Upvotes: 1

Views: 3247

Answers (3)

Benjamin
Benjamin

Reputation: 721

Following on from @LinPy's comment, adapting my Dockerfile per the puppeteer documentation worked:

FROM alpine:edge

# Installs latest Chromium (89) package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      freetype-dev \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      nodejs-npm \
      yarn


# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

# Puppeteer v6.0.0 works with Chromium 89.
RUN yarn add [email protected]
...

As well I added args: ['--no-sandbox', '--disable-setuid-sandbox'] to the Scraper constructor. Thanks

Upvotes: -1

hoangdv
hoangdv

Reputation: 16147

Install firefox execution for your container:

FROM node:14

RUN apt-get update \
    && apt-get install -y wget gnupg fonts-ipafont-gothic fonts-freefont-ttf firefox-esr --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

RUN PUPPETEER_PRODUCT=firefox npm install puppeteer

COPY . .

CMD [ "node", "src/index.js" ]

Upvotes: 2

LinPy
LinPy

Reputation: 18608

the error state that there is errors by running chrome not Firefox .... so make sure that you have all chrome dependencies installed in your Container:

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 

source : Here

Upvotes: 1

Related Questions