How to run Browsershot and zenika alpine-chrome in two different docker images?

  browsershot, docker, mount, php, puppeteer

Goal
Using the zenika alpine-chrome:with-puppeteer docker image like a service to create PDF’s from HTML with Browsershot in a php container.

Description
Inside my dockerized php application i generate pdf files from html. At the moment inside the docker php image it is also installed npm, nodejs, puppeteer and chromium and Browsershot works. But i want to have a seperate image for the npm/nodejs/puppeteer/chromium stuff. Therefore i tried zenika alpine-chrome:with-puppeteer.
I mounted /usr directory of this docker image into my php docker image and when i exec into the php container i can verify that it works. So i can see all the neccessary files like node, chromium-browser or npm.

Problem
At the moment i don’t get any feedback from browsershot. I tried debugging it with xdebug, but i don’t get any error messages and the process just fills my RAM until my host machine stucks.

Question(s)
Is it in general possible to mount folders and execute a process there or is it just for "file sharing"?
If it is possible, what about the permissions? How to handle them?
What is the correct way, to point to the binaries and paths in browsershot?

Here are cuttings from my code:
(naturally i can add more, if more is needed)

Dockerfile (php_dockerfile)

...
FROM php:8.0.2-fpm-alpine AS php-base

ENV PUPPETEER_EXECUTABLE_PATH="usr/src/chrome/bin/chromium-browser"

...

...
FROM zenika/alpine-chrome:with-puppeteer AS chromium
...

docker-compose.yaml

volumes:
  ...
  volume-chrome:
  ...

  ...
chrome:
    container_name: ${COMPOSE_PROJECT_NAME}_${APP_ENVIRONMENT}_chrome
    build:
      context: ./..
      target: chromium
      <<: *php_dockerfile
    volumes:
      - volume-chrome:/usr
    security_opt:
      - seccomp:./scripts/dev/chrome/chrome.json
    restart: always
    command: [ chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222" ]
    ports:
      - "9222:9222"
    networks:
      - backend_net

...

...

php:
    container_name: ${COMPOSE_PROJECT_NAME}_${APP_ENVIRONMENT}_php
    build:
      context: ./..
      target: php-dev
      <<: *php_dockerfile
    volumes:
      - volume-data:/var/www/${APP_DOMAIN}/current
      - type: volume
        target: /usr/src/chrome
        source: volume-chrome
    environment: &php_env

...

PHP code
(maybe the fault is here)

$pdf = $this->browsershot
            ->noSandbox()
            ->setIncludePath('$PATH:/usr/src/chrome/bin')
            ->setNodeModulePath('/usr/src/chrome/src/app/node_modules/')
            ->setChromePath('usr/src/chrome/bin/chromium-browser')
            ->setNodeBinary('usr/src/chrome/bin/node')
            ->setNpmBinary('usr/src/chrome/bin/npm')
            ->showBackground()
            ->setOption('addStyleTag', $this->getStyleSheet())
            ->format('A4')
            ->margins(0.0, 0.0, 0.0, 0.0)
            ->setHtml($html)
            )->pdf();

Source: Docker Questions

LEAVE A COMMENT