1

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.\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n"}]
}

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

Hasunohana
  • 565
  • 8
  • 22

1 Answers1

0

follow this Node.js + Puppeteer on Docker, No usable sandbox answer, I got this

docker-compose:

version: '3.7'
services:
  api:
    build: .
    ports:
      - "3000:3000"
    container_name: web_scraping

Dockerfile:

FROM node:12

RUN  apt-get update \
     && apt-get install -y wget gnupg ca-certificates \
     && 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 \
     # We install Chrome to get all the OS level dependencies, but Chrome itself
     # is not actually used as it's packaged in the node puppeteer library.
     # Alternatively, we could could include the entire dep list ourselves
     # (https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix)
     # but that seems too easy to get out of date.
     && apt-get install -y google-chrome-stable \
     && rm -rf /var/lib/apt/lists/* \
     && wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \
     && chmod +x /usr/sbin/wait-for-it.sh

WORKDIR /app

COPY package.json ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn","start"]

And Add this args in launch instance:

const browser = await puppeteer.launch({
  args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
  ],
});
Hasunohana
  • 565
  • 8
  • 22