1

Im trying to run a Puppeteer script in a docker container with Xfvb so that I can run headless: false on my production app, which is the only way my script will get the required output from the site I am scraping. Im having trouble getting the docker image to run after Ive built it. I originally following this article's process: http://www.smartjava.org/content/using-puppeteer-in-docker-copy-2/ but I was getting the error Error: Could not find browser revision 818858 Run "PUPPETEER_PRODUCT=firefox npm install" once I tried to run the image, which didnt make much sense to me but it seems like the bundled version of Chromium didnt have proper dependencies according to this article

Running Puppeteer in Docker: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-in-docker

So I modified my dockerfile with the ideas they used in their example, which allowed me to build my container with no errors. But when I run the image I get an error that Im stumped on. I believe it has to do with Xfvb.

_XSERVTransmkdir: ERROR: euid != 0,directory /tmp/.X11-unix will not be created.

Dockerfile:

FROM node:latest

# update and add all the steps for running with xvfb
RUN apt-get update &&\
apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
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 \
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget \
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps

# this installs the necessary libs to make the bundled version of Chromium 
# that Puppeteer installs, work.
RUN apt-get update \
    && apt-get install -y 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 \
    && rm -rf /var/lib/apt/lists/*

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# add the required dependencies
WORKDIR /app

COPY node_modules /app/node_modules

RUN npm install puppeteer \
# Add user so we don't need --no-sandbox.
    # same layer as npm install to keep re-chowned files from using up several hundred MBs more space
    && groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
    && mkdir -p /home/pptruser/Downloads \
    && chown -R pptruser:pptruser /home/pptruser 
    # && chown -R pptruser:pptruser /node_modules

# Run everything after as non-privileged user.
USER pptruser

# Finally copy the build application
COPY . .

# make sure we can run without a UI
ENV DISPLAY :99
CMD Xvfb :99 -screen 0 1024x768x16 & node ./src/export.js

Package.json

{
  "name": "puppeteer-headless",
  "version": "1.0.0",
  "description": "Headless crawler with simulated UI",
  "devDependencies": {
    "@types/node": "^14.4.0",
    "@types/puppeteer": "^5.4.0"
  },
  "dependencies": {
    "puppeteer": "^5.5.0"
  }
}

launch method in my script:

const browser = await puppeteer.launch({ 
        headless: false,
        executablePath: 'google-chrome-stable'
       });
behindClouds
  • 81
  • 1
  • 9

0 Answers0