bazel-contrib / rules_nodejs

NodeJS toolchain for Bazel.
https://bazelbuild.github.io/rules_nodejs/
Apache License 2.0
724 stars 523 forks source link

Can't run nodejs_image with Puppeteer #2979

Closed flolu closed 2 years ago

flolu commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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