Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Can't run nodejs_image with Puppeteer #2979

Closed
flolu opened this issue Sep 23, 2021 · 3 comments
Closed

Can't run nodejs_image with Puppeteer #2979

flolu opened this issue Sep 23, 2021 · 3 comments

Comments

@flolu
Copy link
Contributor

flolu commented Sep 23, 2021

I want to run Puppeteer in a Node.js app on a Kubernetes cluster.
This is my setup:

ts_config(
    name = "tsconfig",
    src = "tsconfig.lib.json",
)

ts_project(
    name = "lib",
    srcs = ["index.ts"],
    declaration = True,
    tsconfig = "tsconfig",
    deps = [
        "@npm//@types/node",
        "@npm//puppeteer",
    ],
)

nodejs_binary(
    name = "server",
    data = [
        "lib",
    ],
    entry_point = "index.ts",
)

nodejs_image(
    name = "base_image",
    binary = "server",
)

Everything works fine when running the image locally on my Ubuntu machine.
But the image doesn't run in Docker:

(node:44) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
/app/server.runfiles/drakery/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory

TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

Some dependencies are not available in the Docker container.

With a Dockerfile, they could be installed like this.

How can I add such "apt-get install dependencies" to my nodejs_image?


I've tried to change base image of nodejs_image to a custom-built image via container_pull. But this resulted in some weird "No permission" errors.

A minimal reproduction of the issue can be found here: https://github.com/flolu/bazel-node-puppeteer

@alexeagle
Copy link
Collaborator

yeah chromium requires some shared libraries on the machine, these dependencies aren't captured by bazel, so a custom base image is the right answer. The "No Permission" errors are the things to diagnose. Does that help? you can update the repro to point to some image that has the needed dynamic shared objects.

@flolu
Copy link
Contributor Author

flolu commented Sep 24, 2021

I've updated the repro to use this (drakery/node-puppeteer) as a base image.

Running yarn bazelisk run //:base_image throws

[link_node_modules.js] An error has been reported: [Error: EACCES: permission denied, symlink '/app/server.runfiles/npm/node_modules' -> 'node_modules'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'symlink',
  path: '/app/server.runfiles/npm/node_modules',
  dest: 'node_modules'
} Error: EACCES: permission denied, symlink '/app/server.runfiles/npm/node_modules' -> 'node_modules'

@flolu flolu changed the title Can't run nodejs_image with Puppeteer in Kubernetes Can't run nodejs_image with Puppeteer Sep 24, 2021
@flolu
Copy link
Contributor Author

flolu commented Sep 27, 2021

Fixed it by using this Dockerfile as a base:

FROM ubuntu:20.04

# Install Node.js
RUN apt-get update \
  && apt-get install -y curl
RUN curl --silent --location https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential

# Install Chrome
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
  && apt-get install -y wget gnupg \
  && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
  && 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 -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
  --no-install-recommends \
  && rm -rf /var/lib/apt/lists/*

I pulled it in Bazel like this:

load("@io_bazel_rules_docker//container:container.bzl", "container_pull")

container_pull(
    name = "ubuntu",
    digest = "sha256:a1ceb3aac586b6377821ffe6aede35c3646649ee5ac38c3566799cd04745257f",
    registry = "docker.io",
    repository = "drakery/node-puppeteer",
)

And used it like this:

nodejs_image(
    name = "custom_ubuntu",
    base = "@ubuntu//image",
    binary = "server",
)

Here is the final working repository: https://github.com/flolu/bazel-node-puppeteer/tree/050376d36bccb67a93933882a459f0af3051eabd

@flolu flolu closed this as completed Sep 27, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants