Running Puppeter inside Docker Container

  docker, puppeteer

I’m trying to run my application in container, this is my Dockerfile:

FROM node:12-slim

WORKDIR /app

# Install  Chrome
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/*


RUN yarn add 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

USER pptruser

CMD ["google-chrome-stable"]

COPY package.json ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn","start"]

I wrap my puppeter function with try catch, and when I try to run the application the catch is trigged:

try{
 ...
}catch(error){
  Running as root without --no-sandbox is not supported. See https://crbug.com/638180.nnnTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.mdn"}]
}

I add this arg const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });

My docker-compose:

version: '3.7'
services:
  api:
    build: .
    ports:
      - "3000:3000"
    cap_add:
      - SYS_ADMIN
    init: true
    container_name: web_scraping

I’m running the docker-compose, none erro appers while it is running, only when I call the api the catch is trigged

Source: Docker Questions

LEAVE A COMMENT