Reputation: 65
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?
Upvotes: 3
Views: 5725
Reputation: 1
use thios in Dockerfile
RUN apk add --no-cache \ chromium
and
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: "/usr/bin/chromium",
});
in your code
Upvotes: 0
Reputation: 3695
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 [email protected]
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',
})
Upvotes: 0
Reputation: 375
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.
Upvotes: 0