简体   繁体   中英

Puppeteer fails to launch chromium in docker container

This is my dockerfile code:

FROM node:18-alpine As base

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

RUN apk update && apk add curl gnupg \
    && curl --location --silent https://dl-ssl.google.com/linux/linux_signing_key.pub | apk add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apk/sources.list.d/google.list' \
    && apk update \
    && apk add google-chrome-stable --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main \
    && rm -rf /var/cache/apk/*

USER node
WORKDIR /app

COPY --chown=node:node package*.json .

RUN npm ci

COPY --chown=node:node . .

My backend is running inside a Docker container and is built with Nest Js. I want to save the PDF file after converting the HTML content. I'm using the Puppeteer library for that, and in order for my conversion to work, I need to run Chromium (whether it is headless or not).

When we run the command "npm i puppeteer," I assumed I didn't need to install Chrome. It worked during development, but for some reason it does not work in a Docker container.

I am not sure that the code in my Docker file will work to install Chrome. I got this code online and pasted it in my Docker file, but it shows the error在此处输入图像描述

any solution to this?

I believe you're using an outdated command, try:

RUN apt-get update && apt-get install gnupg wget -y && \
  wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
  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 google-chrome-stable -y --no-install-recommends && \
  rm -rf /var/lib/apt/lists/*

Totally just a hunch, but I'm assuming you're using this tutorial: https://dev.to/cloudx/how-to-use-puppeteer-inside-a-docker-container-568c

But it looks like you're using the older solution.

I have a solution I finally got working in Docker.

The current linux user has to be given permissions and puppeteer configured using the configuration file

Also important is to set the executablePath to /opt/google/chrome/google-chrome and use --no-sandbox mode

using puppeteer@20.5.0

make sure to use the relevant official docker image for puppeteer, in this case ghcr.io/puppeteer/puppeteer:20.5.0

.puppeteerrc.cjs

const { join } = require('path')

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
  cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
}

Dockerfile

FROM ghcr.io/puppeteer/puppeteer:20.5.0

EXPOSE 8080

WORKDIR /app

RUN chown -Rh $user:$user /app
USER $user

COPY package*.json ./
COPY yarn.lock ./

RUN yarn

COPY . .

RUN yarn build

CMD ["yarn", "start"]

index.js

  const browser = await puppeteer.launch({
    headless: 'new',
    slowMo: 25,
    args: [
      '--start-maximized',
      '--no-sandbox',
    ],
    executablePath: '/opt/google/chrome/google-chrome',
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM