I'm unable to run my unit tests on Puppeteer inside of a Docker container. My host is a MacOS with Apple Silicon M1 (arm64) chip. I've also tried to follow the instructions from the Puppeteer Github documentation but they are meant for amd64 instead. Any suggestion?
CodePudding user response:
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.