score:5

Accepted answer

installing puppeteer npm package directly won't work for some reason and sadly the official puppeteer documentation in github is not compatible with arm64 architecture.

this is how i've prepared my dockerfile:

from node:16

run apt-get update \
 && apt-get install -y chromium \
    fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
    --no-install-recommends
    
user node # non-root user that comes with `node` images.

workdir /app

copy --chown=node package.json .
copy --chown=node package-lock.json .

env puppeteer_skip_chromium_download true
env puppeteer_executable_path /usr/bin/chromium

run npm install

copy --chown=node . /app

the dockerfile as it is configured is native to apple silicon (linux/arm64) as well as native for amd64 (linux/amd64) architectures.

you also must pass --no-sandbox argument to the browser on your code:

export async function createbrowserinstance(): promise<puppeteer.browser> {
    return await puppeteer.launch({
        args: ['--no-sandbox'], // required.
        headless: true,
    });
}

the extra flag disables the browser's dev sandboxing, so make sure to access only trusted pages. otherwise it's fine. please, somebody from the future, please feel free to edit this answer or comment how to get rid of the --no-sandbox argument.


Related Query

More Query from same tag