UPDATE ABOUT CLOSED QUESTION:
So I posted this question because all the solutions in the provided links did not work for me especially in the Docker environment, the code works outside Docker environment but doesn't work inside Docker environment, and this is why I was forced to create my own question sharing the current specific code and structure I am using which is not working for me.
I hope this is a good enough reason to reopen the question. Thanks.
Also, I've made update to my Dockerfile below to include some of the new things I've tried from the answers suggested which didn't work regardless.
I am trying to generate a PDF with Puppeteer inside a Docker environment but it doesn't work.
My code looks like this:
Dockerfile
FROM node:slimENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD trueRUN 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/*WORKDIR /appCOPY package.json package-lock.json ./RUN npm installRUN npx puppeteer browsers install chromeRUN apt-get upgradeRUN apt-get updateRUN apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wgetRUN apt-get install -y libgbm-devRUN apt-get install -y libglib2.0-0COPY . .EXPOSE 3000CMD npm start
docker-compose.yml
version: "3.4"services: puppeteer: platform: linux/amd64 image: ghcr.io/puppeteer/puppeteer:latest volumes: - .:/app working_dir: /app report: platform: linux/amd64 container_name: report build: context: . dockerfile: Dockerfile restart: "no" ports: - 3000:3000 volumes: - .:/app - /app/node_modules/
index.js
var express = require("express");var router = express.Router();const puppeteer = require("puppeteer");router.get("/pdf", async function (req, res, next) { let html = fs.readFileSync("views/template/index.html", "utf8"); const headerHTML = fs.readFileSync("views/template/header.html", "utf8"); const data = require("../extract.json"); const header = Handlebars.compile(headerHTML)(dataExtractor(data)); html = Handlebars.compile(html)(dataExtractor(data)); const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"], }); const page = await browser.newPage(); await page.setContent(html); const result = await page.pdf({ headerTemplate: header, displayHeaderFooter: true, preferCSSPageSize: true, margin: { top: "47mm", right: "0mm", bottom: "4mm", left: "0mm", }, footerTemplate:'<div style="width: 100%;"><p style="color: #444; margin: 0 auto;">Page {{page}}</p></div>', format: "A4", }); res.setHeader("Content-Type", "application/pdf"); res.setHeader("Content-Type", "application/pdf"); res.setHeader("Content-Disposition", "attachment; filename=output.pdf"); res.send(result);});module.exports = router;
When I run the code above using docker-compose up
. This is the error I get in my console:
report | /app/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:267report | reject(new Error([report | ^report | report | Error: Failed to launch the browser process!report | qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumpedreport | qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumpedreport | [60:83:0320/121207.974805:ERROR:file_path_watcher_inotify.cc(337)] inotify_init() failed: Function not implemented (38)report | [60:86:0320/121208.037150:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directoryreport | [0320/121208.131701:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)report | Assertion failed: p_rcu_reader->depth != 0 (/qemu/include/qemu/rcu.h: rcu_read_unlock: 101)report | report | report | TROUBLESHOOTING: https://pptr.dev/troubleshootingreport | report | at ChildProcess.onClose (/app/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:267:24)report | at ChildProcess.emit (node:events:531:35)report | at ChildProcess._handle.onexit (node:internal/child_process:294:12)report | report | report | Node.js v21.7.1report | report | [nodemon] app crashed - waiting for file changes before starting...
How can I fix this error and make puppeteer work successfully with Docker Compose?